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);

    }
};
相关推荐
blackicexs19 分钟前
第五周第五天
算法
不吃橘子的橘猫34 分钟前
《集成电路设计》复习资料2(设计基础与方法)
学习·算法·fpga开发·集成电路·仿真·半导体
学无止境_永不停歇36 分钟前
十一、C++11列表初始化、右值引用和移动语义
开发语言·c++
halen3331 小时前
How Masters Tool Fixed My Digital Disaster
算法·均值算法·推荐算法
mjhcsp1 小时前
C++ 背包DP解析
开发语言·c++
重生之后端学习1 小时前
78. 子集
java·数据结构·算法·职场和发展·深度优先
摸鱼仙人~1 小时前
0-1背包与完全背包:遍历顺序背后的秘密
人工智能·算法
juleskk1 小时前
2.15 复试训练
开发语言·c++·算法
那起舞的日子1 小时前
斐波那契数列
java·算法