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;
    }
};
相关推荐
励志的小陈1 小时前
数据结构--二叉树知识讲解
数据结构
自信150413057592 小时前
重生之从0开始学习c++之模板初级
c++·学习
leobertlan2 小时前
好玩系列:用20元实现快乐保存器
android·人工智能·算法
青梅橘子皮2 小时前
C语言---指针的应用以及一些面试题
c语言·开发语言·算法
笨笨饿2 小时前
#58_万能函数的构造方法:ReLU函数
数据结构·人工智能·stm32·单片机·硬件工程·学习方法
历程里程碑2 小时前
2. Git版本回退全攻略:轻松掌握代码时光机
大数据·c++·git·elasticsearch·搜索引擎·github·全文检索
极客智造2 小时前
深度解析 C++ 类继承与多态:面向对象编程的核心
c++
_深海凉_3 小时前
LeetCode热题100-有效的括号
linux·算法·leetcode
零号全栈寒江独钓5 小时前
基于c/c++实现linux/windows跨平台获取ntp网络时间戳
linux·c语言·c++·windows
CSCN新手听安5 小时前
【linux】高级IO,以ET模式运行的epoll版本的TCP服务器实现reactor反应堆
linux·运维·服务器·c++·高级io·epoll·reactor反应堆