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

    }
};
相关推荐
D_evil__3 分钟前
【Effective Modern C++】第六章 lambda表达式:34. 考虑lambda而非bind
c++
沪漂阿龙4 分钟前
大模型采样策略终极指南:Top-k、Top-p与结构化输出最佳实践
人工智能·算法·机器学习
DeepModel6 分钟前
【回归算法】局部加权回归(LWR)详解
人工智能·算法·回归
水木姚姚8 分钟前
string类(C++)
开发语言·c++·windows·vscode·开发工具
方便面不加香菜9 分钟前
C++ 类和对象(一)
开发语言·c++
浅念-15 分钟前
C++ STL list 容器
开发语言·数据结构·c++·经验分享·笔记·算法·list
重生之后端学习17 分钟前
39. 组合总和
java·数据结构·算法·职场和发展·深度优先
Frostnova丶19 分钟前
LeetCode 868. 二进制间距
算法·leetcode
nix.gnehc19 分钟前
深入理解Go并发核心:GMP模型与Goroutine底层原理
开发语言·算法·golang
心本无晴.26 分钟前
RAG中的混合检索(Hybrid Search):稀疏检索与稠密检索的强强联合
人工智能·python·算法