统计完全子字符串

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

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;
    }
};
相关推荐
木卫二号Coding14 小时前
Python-文件拷贝+文件重命名+shutil+记录
开发语言·python
leaves falling14 小时前
冒泡排序(基础版+通用版)
数据结构·算法·排序算法
老鼠只爱大米14 小时前
LeetCode算法题详解 56:合并区间
leetcode·并查集·合并区间·区间合并·线性扫描·算法面试
bubiyoushang88814 小时前
基于Q-learning的路径规划MATLAB仿真程序实现
开发语言·matlab
蜗牛去旅行吧14 小时前
面试宝典集锦
面试·职场和发展
C雨后彩虹14 小时前
无向图染色
java·数据结构·算法·华为·面试
FAFU_kyp14 小时前
Rust 结构体(struct)
开发语言·后端·rust
坚持就完事了14 小时前
扫描线算法
算法
努力写代码的熊大14 小时前
深入探索C++关联容器:Set、Map、Multiset与Multimap的终极指南及底层实现剖析
开发语言·c++
鱼跃鹰飞14 小时前
Leetcode尊享面试100题:252. 会议室
算法·leetcode·面试