Maximum Number of Vowels in a Substring of Given Length LeetCode solution in Java

1
class Solution {
2
public int maxVowels(String s, int k) {
3
int n = s.length();
4
int maxVowels = 0;
5
int count = 0;
6

7
int[] vowels = new int[128];
8
vowels['a'] = 1;
9
vowels['e'] = 1;
10
vowels['i'] = 1;
11
vowels['o'] = 1;
12
vowels['u'] = 1;
13

14
for (int i = 0; i < k; i++) {
15
count += vowels[s.charAt(i)];
16
}
17

18
maxVowels = count;
19
for (int i = k; i < n; i++) {
20
count += vowels[s.charAt(i)] - vowels[s.charAt(i - k)];
21
maxVowels = Math.max(maxVowels, count);
22
// System.out.println(Arrays.toString(vowels));
23
if (maxVowels == k) {
24
return maxVowels;
25
}
26
}
27
return maxVowels;
28
}
29
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0