统计完全子字符串

很不错的计数问题,用到了分组循环技巧和滑动窗口 代码的实现方式也非常值得多看

cpp 复制代码
class Solution {
public:
    int f(string s,int k){
        int res = 0;
        for(int m=1;m<=26&&k*m<=s.size();++m){
            int cnt[27]{};
            auto check=[&](){
                for(int i=0;i<=26;++i){
                    if(cnt[i]&&cnt[i]!=k)return;
                }
                res++;
            };

            for(int r=0;r<s.size();++r){
                cnt[s[r]-'a']++;
                int l = r+1-k*m;
                if(l>=0){
                    check();
                    cnt[s[l]-'a']--;
                }
            }
        }
        return res;
    }
    int countCompleteSubstrings(string word, int k) {
        int n = word.size();
        int res = 0;
        for(int i=0;i<n;){
            int start = i;
            ++i;
            while(i<n&&abs(int(word[i]-word[i-1]))<=2)++i;
            res+=f(word.substr(start,i-start),k);
        }
        return res;
    }
};
相关推荐
apcipot_rain3 小时前
C++转Java日记(04)
java·开发语言·c++
NEFU_Jyj4 小时前
牛客周赛R168
算法
云和数据.ChenGuang5 小时前
LangGraph 异步 Node 的设计与写法实践
开发语言·人工智能·python·机器学习·php
WL学习笔记6 小时前
vector模拟实现
c++
蜗牛一步步往上爬8 小时前
第十六章 C++语言在深度学习中的应用
开发语言·c++·深度学习
张张123y10 小时前
DSH 插件调试避坑:client.js 设置面板、热重载与高频报错排查表
开发语言·javascript·人工智能·开源·ecmascript·后端开发·ai agent
likexuec10 小时前
数据结构(2)
数据结构
文丑颜不良啊11 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z11 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode