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

    }
};
相关推荐
luj_176810 分钟前
大律师考核应重能力与科技素养
c语言·开发语言·c++·经验分享·算法
无定义_15 分钟前
Floyd——Warshall
算法
刃神太酷啦33 分钟前
Linux 系统 MySQL 完整安装配置教程:从卸载 MariaDB 到优化 my.cnf----《Hello MySQL!》(1)
android·linux·c语言·c++·mysql·leetcode·mariadb
带多刺的玫瑰1 小时前
Leecode#9刷题之回文数
数据结构·算法
泯泷1 小时前
手搓JSVM第 5 篇:写第一个编译器:从 AST 生成 IR
前端·javascript·算法
泯泷2 小时前
手搓JSVM第 7 篇:控制流:if、while 与 jump
前端·javascript·算法
泯泷2 小时前
手搓JSVM第 6 篇:把 IR 编成字节码:emit 与 label fixup
前端·javascript·算法
泯泷2 小时前
手搓JSVM第 3 篇:从栈式 VM 到寄存器式 VM:为什么我们选择寄存器
前端·javascript·算法
泯泷2 小时前
第 4 篇:让 VM 支持变量:Slot、Environment 与 TDZ
前端·javascript·算法
Asize2 小时前
54. 螺旋矩阵
算法