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

    }
};
相关推荐
冷小鱼3 分钟前
AI Agent 核心算法:任务规划(Planning)的深度技术解析
人工智能·算法·planning
杜子不疼.8 分钟前
【C++ 在线五子棋对战】- 会话管理模块实现
开发语言·c++
退休倒计时11 分钟前
【每日一题】LeetCode 78. 子集 TypeScript
算法·leetcode·typescript
有点。12 分钟前
C++深度优先搜索(DFS)的概念(一)
开发语言·c++·深度优先
旖-旎15 分钟前
《LeetCode 978 最长湍流子数组 || LeetCode 139 单词拆分》
c++·算法·leetcode·动态规划
aaPIXa62220 分钟前
C++大型项目模块化拆分实战记录
开发语言·c++
cxr82825 分钟前
第16章 跨域迁移与能力融合——从专家到通才
人工智能·算法·智能体·hermes
石山代码29 分钟前
C++23 新特性在 CLion 中的实战体验
开发语言·c++·c++23
学究天人31 分钟前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(补充卷9)
人工智能·线性代数·算法·数学建模·动态规划·原型模式·抽象代数
Keven_1138 分钟前
算法札记:图的存储方式
算法·图论