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

    }
};
相关推荐
sqyno1sky8 分钟前
C++中的契约编程
开发语言·c++·算法
优化控制仿真模型20 分钟前
2026年最新驾考科目一考试题库2309道全。电子版pdf
经验分享·算法·pdf
qq_3349031523 分钟前
嵌入式C++驱动开发
开发语言·c++·算法
阿贵---34 分钟前
C++代码规范化工具
开发语言·c++·算法
暮冬-  Gentle°42 分钟前
自定义内存检测工具
开发语言·c++·算法
ccLianLian1 小时前
数论·欧拉函数
数据结构·算法
2501_945424801 小时前
C++编译期矩阵运算
开发语言·c++·算法
2301_815482931 小时前
C++中的类型标签分发
开发语言·c++·算法
xushichao19891 小时前
代码生成优化技术
开发语言·c++·算法
炽烈小老头1 小时前
【每天学习一点算法 2026/03/22】前 K 个高频元素
学习·算法