文章目录
- 题目描述
- 题解思路
- 题解代码
- 题目链接
题目描述
题解思路
我们遍历长度为k的窗口,我们只需要记录窗口内的元音字母数量即可,遍历过程中刷新最大数目
题解代码
impl Solution {pub fn max_vowels(s: String, k: i32) -> i32 {let s = s.as_bytes();let k = k as usize;let mut count = 0;fn is_vowel(c: u8) -> bool {c == b'a' || c == b'e' || c == b'i' || c == b'o' || c == b'u'}// 填满长度为k的窗口for i in 0..k {if is_vowel(s[i]) {count += 1;}}let mut ans = count;for i in k..s.len() {// 加入窗口if is_vowel(s[i]) {count += 1;}// 移出窗口if is_vowel(s[i - k]) {count -= 1;}ans = ans.max(count);}ans}
}
题目链接
https://leetcode.cn/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/