Problem Statement (Source):
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.
Tip
This question was also solved using the same template in 643. Maximum Average Subarray I.
class Solution {
public int maxVowels(String s, int k) {
int start = 0;
int end = 0;
int size = s.length();
String vowels = "aeiou";
int max = 0;
int vC = 0; //vowel Count
while(end < size){
Character ch = s.charAt(end);
if(vowels.indexOf(ch) != -1){
vC++;
}
if(end - start + 1 == k){
max = Math.max(max, vC);
if(vowels.indexOf(s.charAt(start)) != -1){
vC--;
}
start++;
}
end++;
}
return max;
}
}