LeetCode 1456 定长子串中元音的最大数目
方法1:滑动窗口
class Solution {
public int maxVowels(String s, int k) {
int n = s.length(), count = 0, ans = 0;
for (int i = 0; i < n; i++) {
count += isVowel(s.charAt(i));
if (i >= k) {
count -= isVowel(s.charAt(i - k));
}
ans = Math.max(count, ans);
}
return ans;
}
public int isVowel(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ? 1 : 0;
}
}
class Solution:
def maxVowels(self, s: str, k: int) -> int:
count = 0; ans = 0
for i, ch in enumerate(s):
count += 1 if ch in "aeiou" else 0
if i >= k:
count -= 1 if s[i - k] in "aeiou" else 0
ans = max(ans, count)
return ans
标签:count,ch,窗口,int,ans,滑动
From: https://www.cnblogs.com/XuGui/p/18374035