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

    }
};
相关推荐
YGGP1 小时前
【Golang】LeetCode 128. 最长连续序列
leetcode
你撅嘴真丑7 小时前
第九章-数字三角形
算法
在路上看风景7 小时前
19. 成员初始化列表和初始化对象
c++
uesowys7 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
zmzb01037 小时前
C++课后习题训练记录Day98
开发语言·c++
ValhallaCoder7 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮7 小时前
AI 视觉连载1:像素
算法
念风零壹8 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
智驱力人工智能8 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥9 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法