统计完全子字符串

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

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;
    }
};
相关推荐
大可不必了9 小时前
03 函数定义和调用
开发语言·python
I_belong_to_jesus9 小时前
C++关于分支预测的优化1
开发语言·c++·算法
承渊政道10 小时前
【Python编程—从入门到实践】(记分与闯关:用Python+Pygame,打造越战越精彩的太空大战!)
开发语言·python·pycharm·游戏程序·pygame
爱学习的程序媛10 小时前
Python常用框架梳理
开发语言·后端·python·ai·web·后端框架·python框架
Rabitebla10 小时前
【Linux】进程控制(十二) 自定义 Shell(二):内建命令与进程工作目录管理
linux·c++·笔记·学习·bash
eggwyw12 小时前
Anaconda安装卸载的详细流程指南
开发语言·python
码客-ygluu16 小时前
一个通用游戏数据服务器
c++·架构
another heaven16 小时前
【C/C++ Ranges 是零开销抽象吗?拆开管道、惰性求值和编译期膨胀】
c语言·开发语言·c++
Code_Solitude16 小时前
C语言动态内存 + 柔性数组 + 数组名退化
c语言·算法·柔性数组