-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxVowels.java
More file actions
35 lines (25 loc) · 863 Bytes
/
Copy pathMaxVowels.java
File metadata and controls
35 lines (25 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package LeetcodePractice;
public class MaxVowels {
public int maxVowels(String s, int k) {
int maxCount = 0, currentCount = 0;
for (int i = 0; i < s.length(); i++) {
if (isVowel(s.charAt(i))) {
currentCount++;
}
if (i >= k && isVowel(s.charAt(i - k))) {
currentCount--;
}
maxCount = Math.max(maxCount, currentCount);
}
return maxCount;
}
private boolean isVowel(char c) {
return "aeiou".indexOf(c) != -1;
}
public static void main(String[] args) {
MaxVowels sol = new MaxVowels();
System.out.println(sol.maxVowels("abciiidef", 3));
System.out.println(sol.maxVowels("aeiou", 2));
System.out.println(sol.maxVowels("leetcode", 3));
}
}