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

    }
};
相关推荐
业精于勤的牙12 小时前
浅谈:算法中的斐波那契数(二)
算法·职场和发展
不穿格子的程序员13 小时前
从零开始写算法——链表篇4:删除链表的倒数第 N 个结点 + 两两交换链表中的节点
数据结构·算法·链表
liuyao_xianhui13 小时前
寻找峰值--优选算法(二分查找法)
算法
dragoooon3413 小时前
[hot100 NO.19~24]
数据结构·算法
神仙别闹13 小时前
基于QT(C++)实现学本科教务系统(URP系统)
数据库·c++·qt
deng-c-f14 小时前
Linux C/C++ 学习日记(49):线程池
c++·学习·线程池
ulias21214 小时前
C++ 的容器适配器——从stack/queue看
开发语言·c++
daidaidaiyu14 小时前
FFmpeg 关键的结构体
c++·ffmpeg
Tony_yitao14 小时前
15.华为OD机考 - 执行任务赚积分
数据结构·算法·华为od·algorithm