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;
    }
};
相关推荐
SoftLipaRZC1 天前
单链表的应用:经典OJ题与通讯录项目实战
数据结构
SoftLipaRZC1 天前
单链表专题:从概念到实现
数据结构
折哥的程序人生 · 物流技术专研1 天前
Java面试85题图解版 · 特别篇:2026后端高频面试题复盘(算法底层逻辑+高并发架构设计全解析,附Java实战代码)
java·网络·数据库·算法·面试
玖玥拾1 天前
C/C++ 基础笔记(十四)多态与模板编程
c语言·c++·多态·模板
想吃火锅10051 天前
【leetcode】14.最长公共前缀js
算法·leetcode·职场和发展
Roann_seo%1 天前
C++文件操作完全指南:从文本读写到二进制文件处理
开发语言·c++
坚果派·白晓明1 天前
【鸿蒙PC】SDL3 适配:AtomCode + Skills 快速集成 NAPI 测试工具
c++·华为·ai编程·harmonyos·atomcode
云絮.1 天前
数据库操作
数据库·mysql·算法·oracle
小林ixn1 天前
LeetCode 206. 反转链表(迭代 + 递归详解)
算法·leetcode·链表
凡人叶枫1 天前
Effective C++ 条款17:以独立语句将 newed 对象置入智能指针
java·linux·开发语言·c++·算法