LeetCode75——Day15

文章目录

一、题目

1456. Maximum Number of Vowels in a Substring of Given Length

Given a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k.

Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.

Example 1:

Input: s = "abciiidef", k = 3

Output: 3

Explanation: The substring "iii" contains 3 vowel letters.

Example 2:

Input: s = "aeiou", k = 2

Output: 2

Explanation: Any substring of length 2 contains 2 vowels.

Example 3:

Input: s = "leetcode", k = 3

Output: 2

Explanation: "lee", "eet" and "ode" contain 2 vowels.

Constraints:

1 <= s.length <= 105

s consists of lowercase English letters.

1 <= k <= s.length

二、题解

利用滑动窗口进行判断

cpp 复制代码
class Solution {
public:
    int maxVowels(string s, int k) {
        int n = s.length();
        unordered_map<char,int> map;
        map['a'] = 1;
        map['e'] = 1;
        map['i'] = 1;
        map['o'] = 1;
        map['u'] = 1;
        int count = 0;
        int res = 0;
        for(int i = 0;i < k;i++){
            if(map[s[i]] == 1) count++;
        }
        res = max(res,count);
        for(int i = k;i < n;i++){
            if(map[s[i - k]] == 1) count--;
            if(map[s[i]] == 1) count++;
            res = max(res,count);
        }
        return res;
    }
};
相关推荐
Doubbbbbbble云18 分钟前
基于流处理框架的实时算法实现策略4
算法
2601_9628857224 分钟前
如何用 Python 计算 ROC 变动率指标做动量分析?
开发语言·python·算法
Logic10127 分钟前
C语言/数据结构动态规划题解:爬楼梯(含坏台阶)——避开障碍物的路径计数
c语言·数据结构·动态规划·时间复杂度·空间复杂度·算法题·爬楼梯
胡家伟++38 分钟前
我用 48 轮 AI 思维链做了一次研究战略推演:从“验证信号危机“到“验证栈“统一框架
人工智能·算法
潼心1412o1 小时前
C++初阶(长期更新)第9讲:string(上)
开发语言·c++
朝朝辞暮i1 小时前
C++ 第 39 章: 阶段性总复习——类、对象、构造、继承、this、指针与智能指针
开发语言·c++·算法·ros2
wuminyu1 小时前
Virtual Thread重投递至ForkJoinPool任务队列过程解析
java·linux·c语言·jvm·c++
垆边人似月.1 小时前
华为题:污染水域
算法
All for pursuit.2 小时前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode
ZhangJun952 小时前
Mobike 共享单车分析项目
人工智能·python·算法·kmeans·聚类·knn