统计完全子字符串

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

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;
    }
};
相关推荐
console.log('npc')1 小时前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
honker_X1 小时前
Python字符串全攻略:从入门到精通
开发语言·python
半杯咖啡半行码1 小时前
Qt 隐式共享(Implicit Sharing)深度解析:机制、陷阱与 Qt6迁移
开发语言·数据库·qt
吃我一窝jio2 小时前
Docker+Jenkins+K8s全流程部署
java·开发语言·笔记·python
eggwyw2 小时前
C++设计模式之解释器模式
开发语言·c++
橘子133 小时前
【高级数据库】关系代数基本概念
算法
mengge.cloud4 小时前
Python基础保姆级!从入门到入土
开发语言·python·云原生·pygame
在爪洼山玩泥巴5 小时前
fastjson2 JSONPath 取不存在路径,为什么会抛 “not support”?
java·开发语言·jsonpath·fastjson
电科_银尘14 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
星落梦生15 小时前
SpringAOP基础笔记(7)
java·开发语言