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;
    }
};
相关推荐
We་ct1 分钟前
LeetCode 228. 汇总区间:解题思路+代码详解
前端·算法·leetcode·typescript
charlee442 分钟前
为什么现代 C++ 库都用 PIMPL?一场关于封装、依赖与安全的演进
c++·智能指针·raii·pimpl·编译防火墙·封装设计
AIpanda8886 分钟前
如何借助AI销冠系统提升数字员工在销售中的成效?
算法
啊阿狸不会拉杆6 分钟前
《机器学习导论》第 7 章-聚类
数据结构·人工智能·python·算法·机器学习·数据挖掘·聚类
木非哲12 分钟前
机器学习--从“三个臭皮匠”到 XGBoost:揭秘 Boosting 算法的“填坑”艺术
算法·机器学习·boosting
MSTcheng.12 分钟前
CANN ops-math算子的跨平台适配与硬件抽象层设计
c++·mfc
code monkey.13 分钟前
【Linux之旅】Linux 进程间通信(IPC)全解析:从管道到共享内存,吃透进程协作核心
linux·c++·ipc
薛定谔的猫喵喵16 分钟前
基于C++ Qt的唐代诗歌查询系统设计与实现
c++·qt·sqlite
Re.不晚16 分钟前
JAVA进阶之路——数据结构之线性表(顺序表、链表)
java·数据结构·链表
小辉同志19 分钟前
437. 路径总和 III
算法·深度优先·广度优先