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;
    }
};
相关推荐
我叫袁小陌8 分钟前
C++内存分布详解
开发语言·c++
代码or搬砖10 分钟前
JVM垃圾回收算法
jvm·算法
Aaron158811 分钟前
基于RFSOC+VU13P在5G波束成形中的技术应用分析报告
人工智能·算法·5g·fpga开发·硬件架构·信息与通信·基带工程
Hi2024021711 分钟前
如何向Virtual Audio Cable写入自定义音频数据
c++·windows·音视频·virtualaudio·虚拟音频线
小丁努力不焦虑12 分钟前
常考算法题
算法
C雨后彩虹14 分钟前
亲子游戏问题
java·数据结构·算法·华为·面试
ht巷子18 分钟前
Qt:QPainter坐标系统和坐标转换
开发语言·c++·qt
HalvmånEver18 分钟前
Linux:基于匿名管道创建出简易进程池(进程间通信五)
linux·运维·服务器·c++·进程池·管道pipe
郝学胜-神的一滴18 分钟前
雕栏玉砌:Qt 自定义窗口之美——标题、圆角、阴影三艺精解
开发语言·c++·qt·程序人生
vyuvyucd21 分钟前
C++ SO库创建与调用全攻略
c++