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

    }
};
相关推荐
你的冰西瓜15 分钟前
C++中的array容器详解
开发语言·c++·stl
2501_9434691523 分钟前
【无标题】
数据结构·算法
_codemonster27 分钟前
计算机视觉入门到实战系列(八)Harris角点检测算法
python·算法·计算机视觉
Snow_day.31 分钟前
有关排列排列组合(1)
数据结构·算法·贪心算法·动态规划·图论
Ccjf酷儿1 小时前
C++语言程序设计 (郑莉)第六章 数组、指针和字符串
开发语言·c++
陌路201 小时前
C++28 STL容器--array
开发语言·c++
dora1 小时前
【开发火星地平线辅助】智商不够,编程来凑
算法
im_AMBER1 小时前
Leetcode 100 在链表中插入最大公约数
数据结构·c++·笔记·学习·算法·leetcode·链表
Z1Jxxx1 小时前
删除字符串2
开发语言·c++·算法
踩坑记录1 小时前
leetcode hot100 15. 三数之和 medium
算法·leetcode·职场和发展