leetcode 3306 C++

因为我很多STL的用法不太会,所以直接看了参考答案,通过算至少k个辅音字母子字符串和至少k+1个辅音字母子字符串的个数,然后相减就是恰好k个辅音字母子字符串的个数。

cpp 复制代码
class Solution {
public:
    long long cnt(string word, int k){
        set<char> v = {'a', 'e','i','o','u'};
        long long res = 0 * 1L;
        int n = word.size();
        map<char, int> tmp;
        int f = 0;
        for(int i = 0, j = 0; i < n; i ++) {
            // int j = i;
            while(j < n && (f < k || tmp.size() < v.size())) {
                if(v.count(word[j])) {
                    tmp[word[j]] ++;
                }
                else {
                    f++;
                }
                j ++;
            }
            if(f >= k && tmp.size() == v.size()) res += (n - j + 1);
            if(v.count(word[i])) {
                tmp[word[i]] --;
                if(tmp[word[i]] == 0) tmp.erase(word[i]);
            }
            else {
                f --;
            }

        }
        return res;

    }
    long long countOfSubstrings(string word, int m) {
        return cnt(word, m) - cnt(word, m + 1);

    }
};
相关推荐
iAkuya5 分钟前
(leetcode)力扣100 57电话号码的字母组合(回溯)
算法·leetcode·深度优先
WarmSword6 分钟前
mac上用cursor/vscode调试root权限进程
c++·ide·vscode·macos·mac
m0_7369191018 分钟前
模板元编程性能分析
开发语言·c++·算法
pen-ai25 分钟前
【YOLO系列】 YOLOv1 目标检测算法原理详解
算法·yolo·目标检测
wbs_scy30 分钟前
C++11:类新功能、lambda与包装器实战
开发语言·c++
永远睡不够的入35 分钟前
类和对象(中)
c++
飞鹰5144 分钟前
深度学习算子CUDA优化实战:从GEMM到Transformer—Week4学习总结
c++·人工智能·深度学习·学习·transformer
2301_765703141 小时前
C++中的职责链模式实战
开发语言·c++·算法
StandbyTime1 小时前
《算法笔记》学习记录-第一章
c++·算法·算法笔记
近津薪荼1 小时前
优选算法——双指针8(单调性)
数据结构·c++·学习·算法