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

    }
};
相关推荐
weixin_4461224614 分钟前
LinkedList剖析
算法
GiraKoo16 分钟前
【GiraKoo】C++14的新特性
c++
悠悠小茉莉27 分钟前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio
坏柠41 分钟前
C++ Qt 基础教程:信号与槽机制详解及 QPushButton 实战
c++·qt
泽02021 小时前
C++之红黑树认识与实现
java·c++·rpc
百年孤独_1 小时前
LeetCode 算法题解:链表与二叉树相关问题 打打卡
算法·leetcode·链表
我爱C编程2 小时前
基于拓扑结构检测的LDPC稀疏校验矩阵高阶环检测算法matlab仿真
算法·matlab·矩阵·ldpc·环检测
算法_小学生2 小时前
LeetCode 75. 颜色分类(荷兰国旗问题)
算法·leetcode·职场和发展
运器1232 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程
算法_小学生2 小时前
LeetCode 287. 寻找重复数(不修改数组 + O(1) 空间)
数据结构·算法·leetcode