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

    }
};
相关推荐
小辉同志14 分钟前
146.LRU缓存
c++·算法·链表·缓存·力扣
shix .21 分钟前
爬虫一些基础知识的备忘录(需要自取)
c++·爬虫·python
mzgong36 分钟前
DeepSeek-R1深度解读
人工智能·深度学习·算法
Vitalia42 分钟前
⭐算法OJ⭐汉明距离【位操作】(C++ 实现)Hamming Distance
开发语言·c++·算法
白云千载尽1 小时前
开源的自动驾驶视觉语言模型标注数据集
算法·机器学习·自动驾驶·ros
小冯的编程学习之路1 小时前
【QT】:QT图形化界面相关准备工作
开发语言·c++·qt
无咎.lsy1 小时前
leetcode【面试经典150系列】(一)
数据结构·算法
奕天者1 小时前
C++学习笔记(十六)——函数重载
c++·笔记·学习
tan180°1 小时前
版本控制器Git(4)
linux·c++·git·后端·vim
whltaoin1 小时前
软考数据结构四重奏:软件工程师的线性、树、图、矩阵算法精要
数据结构·算法