很不错的计数问题,用到了分组循环技巧和滑动窗口 代码的实现方式也非常值得多看
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;
}
};