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

    }
};
相关推荐
fengfuyao98518 分钟前
匈牙利算法的MATLAB实现
java·算法·matlab
路过君_P22 分钟前
C++ 算法题解:迷宫寻路
c++·算法·深度优先
止观止23 分钟前
告别“祖传C++”:开启你的现代C++之旅
c++·c++11·c++20·编程思想·现代c++
罗湖老棍子30 分钟前
二维vector完全指南1:从定义到增删改查
数据结构·c++·算法·stl
再卷也是菜31 分钟前
C++篇(22)LRU Cache
数据结构·c++·算法
语落心生33 分钟前
海量数据集的AI自动化预测打标 -- 振动特征多标签分类
算法
语落心生37 分钟前
海量数据集AI自动化打标 - 温度周期检测
算法
语落心生1 小时前
海量数据集的AI自动化预测打标 -- 矿业音频分类
算法
吃着火锅x唱着歌1 小时前
LeetCode 3185.构成整天的下标对数目II
算法·leetcode·职场和发展