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

    }
};
相关推荐
Wect4 分钟前
LeetCode 72. 编辑距离:动态规划经典题解
前端·算法·typescript
zhouwy1134 分钟前
Poco 与 libevent 网络编程
c++
憨波个12 分钟前
【说话人日志】DOVER-Lap:overlap-aware diarization 输出融合算法
人工智能·深度学习·算法·音频·语音识别
叼烟扛炮19 分钟前
C++第四讲:类和对象(下)
c++·算法·类和对象
Rabitebla20 分钟前
vector 的骨架:三根指针、模板陷阱与迭代器失效的第一现场
开发语言·数据结构·c++·算法
代码不停37 分钟前
BFS解决floodfill算法题目练习
算法·宽度优先
上弦月-编程44 分钟前
C语言指针从入门到实战
java·jvm·算法
WL_Aurora44 分钟前
Python 算法基础篇之树和二叉树
python·算法
txzrxz1 小时前
关于前缀和
算法·动态规划·图论
杨连江1 小时前
载流子矩阵限域束缚实现常温常压超导的理论与结构设计
算法