⭐️⭐️
# 題目敘述
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.
# Solution
class Solution { | |
public: | |
int maxVowels(string s, int k) { | |
int res = 0; | |
unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u'}; | |
for (int i=0, tmp=0; i<s.size(); i++) { | |
tmp += vowels.count(s[i]); | |
if (i - k >= 0) tmp -= vowels.count(s[i-k]); | |
res = max(res, tmp); | |
} | |
return res; | |
} | |
}; |
class Solution { | |
public int maxVowels(String s, int k) { | |
int ans = 0; | |
String temp = s.substring(0, k); | |
for (char t : temp.toCharArray()) { | |
if (isVowel(t)){ | |
ans++; | |
} | |
} | |
int cur = ans; | |
for (int i = k; i < s.length(); i++) { | |
if (isVowel(s.charAt(i))) | |
cur++; | |
if (isVowel(s.charAt(i - k))) | |
cur--; | |
ans = Math.max(ans, cur); | |
} | |
return ans; | |
} | |
public boolean isVowel(char c) { | |
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') | |
return true; | |
return false; | |
} | |
} |