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

    }
};
相关推荐
纵有疾風起24 分钟前
C++——多态
开发语言·c++·经验分享·面试·开源
稚辉君.MCA_P8_Java25 分钟前
Gemini永久会员 Java实现的暴力递归版本
java·数据结构·算法
冯诺依曼的锦鲤37 分钟前
算法练习:差分
c++·学习·算法
有意义1 小时前
栈数据结构全解析:从实现原理到 LeetCode 实战
javascript·算法·编程语言
鹿鹿鹿鹿isNotDefined1 小时前
逐步手写,实现符合 Promise A+ 规范的 Promise
前端·javascript·算法
Mr_WangAndy1 小时前
现代C++模板与泛型编程_第4章_remove_all_sequence,integer_sequence,is_union
c++·c++40周年·c++标准库用法
封奚泽优1 小时前
下降算法(Python实现)
开发语言·python·算法
im_AMBER2 小时前
算法笔记 16 二分搜索算法
c++·笔记·学习·算法
高洁012 小时前
【无标具身智能-多任务与元学习】
神经网络·算法·aigc·transformer·知识图谱
leoufung2 小时前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode