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

    }
};
相关推荐
alphaTao10 小时前
LeetCode 每日一题 2026/5/25-2026/5/31
算法·leetcode
John_ToDebug10 小时前
Chromium Settings 自启动开关:三种 pref 同步方案深度对比
c++·chrome·ai
菜菜的顾清寒10 小时前
力扣HOT100(41)动态规划-杨辉三角
算法·leetcode·动态规划
Cthy_hy10 小时前
Python算法竞赛:集合去重+字典映射 核心用法一站式整理
数据结构·python·算法
还在点灯@10 小时前
基于visual studio的MFC上位机实现界面切换
c++·visualstudio·mfc
Deepoch10 小时前
Deepoc数学大模型:驱动发动机行业数智化转型的底层解
人工智能·算法·deepoc·数学大模型
happymaker062610 小时前
LeetCodeHot100——盛水最多的容器
数据结构·算法·leetcode·双指针·hot100
3DVisionary10 小时前
蓝光三维扫描:磁性轴承全尺寸精密3D检测方案
算法·3d·3d检测·蓝光三维扫描·精密检测·磁性轴承·圆度测量
视图猿人10 小时前
ROS2 JAZZY+Gazebo harmonic小车机器人建模、激光雷达使用、图像传感器使用、构建导航地图、SLAM自动导航仿真
c++·机器人
过期动态10 小时前
【LeetCode 热题 100】三数之和
java·数据结构·算法·leetcode·职场和发展·排序算法