统计完全子字符串

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

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 小时前
15_国际化和本地化:tr()、ts 文件、QM 文件、多语言切换
c++·qt
地平线开发者10 小时前
【模型轻量化专题】衡量模型轻量性的指标
算法
程与留11 小时前
14_Qt 样式表(QSS)入门(语法、选择器、美化实战)
c++·qt
学习星球13 小时前
OFDM技术精讲:正交性推导、循环前缀原理与80行Python链路仿真(附实测数据)
算法·面试·前端框架
欧特克_Glodon19 小时前
OpenCV计算机视觉开发入门与实践<二十七>:图像分割概述
c++·人工智能·opencv·计算机视觉
蒸蒸yyyyzwd19 小时前
cpp 选手秋招学习笔记 day21
c++·面试·八股
alphaTao19 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
Interview Aid11219 小时前
TikTok OA 四题分享|半小时内 AC,题目基本都是实现题
java·开发语言·算法·面试·职场和发展
CoderIsArt1 天前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
顶点多余1 天前
那些在算法中适合巩固的知识点---1
java·前端·算法