统计完全子字符串

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

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;
    }
};
相关推荐
会员源码网1 小时前
构造函数抛出异常:C++对象部分初始化的陷阱与应对策略
c++
有意义2 小时前
深度拆解分割等和子集:一维DP数组与倒序遍历的本质
前端·算法·面试
xlp666hub3 小时前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
用户726876103373 小时前
解放双手的健身助手:基于 Rokid AR 眼镜的运动计时应用
算法
Wect3 小时前
LeetCode 17. 电话号码的字母组合:回溯算法入门实战
前端·算法·typescript
不想写代码的星星4 小时前
static 关键字:从 C 到 C++,一篇文章彻底搞懂它的“七十二变”
c++
xlp666hub20 小时前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
ZhengEnCi1 天前
08c. 检索算法与策略-混合检索
后端·python·算法
程序员小崔日记1 天前
大三备战考研 + 找实习:我整理了 20 道必会的时间复杂度题(建议收藏)
算法·408·计算机考研
任沫1 天前
字符串
数据结构·后端