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

    }
};
相关推荐
CodeWithMe8 分钟前
【读书笔记】《C++ Software Design》第三章 The Purpose of Design Patterns
c++·设计模式
前端拿破轮14 分钟前
字符串消消乐你会吗?😋😋😋
算法·leetcode·面试
EndingCoder34 分钟前
图算法在前端的复杂交互
前端·算法·图算法
kanhaoning1 小时前
将重排序大模型Qwen3-Reranker-8B的知识蒸馏到小模型BGE-reranker-v2-m3上
算法
Thymme1 小时前
C++获取时间和格式化时间
c++
CoovallyAIHub1 小时前
2025年小目标检测分享:从无人机视角到微观缺陷的创新模型
深度学习·算法·计算机视觉
用户40315986396631 小时前
表达式并发计算
java·算法
前端拿破轮1 小时前
不是吧不是吧,2025年了还有人不会括号匹配?
算法·leetcode·面试
CoovallyAIHub2 小时前
无人机图像中的小目标检测新利器:深入解析 LAM-YOLO 模型
深度学习·算法·计算机视觉
真的想上岸啊2 小时前
学习C++、QT---25(QT中实现QCombobox库的介绍和用QCombobox设置编码和使用编码的讲解)
c++·qt·学习