Skip to content

Commit f977fa6

Browse files
committed
Added Maximum Number of Vowels in a Substring of Given Length.java
1 parent 4b29412 commit f977fa6

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int maxVowels(String s, int k) {
3+
Map<Character, Integer> map = new HashMap<>();
4+
String vowels = "aeiou";
5+
int count = 0;
6+
int maxCount = 0;
7+
int start = 0;
8+
int end = 0;
9+
int n = s.length();
10+
while (end < (k - 1)) {
11+
if (vowels.indexOf(s.charAt(end)) != -1) {
12+
map.put(s.charAt(end), map.getOrDefault(s.charAt(end), 0) + 1);
13+
count++;
14+
}
15+
end++;
16+
}
17+
while (end < n) {
18+
if (vowels.indexOf(s.charAt(end)) != -1) {
19+
map.put(s.charAt(end), map.getOrDefault(s.charAt(end), 0) + 1);
20+
count++;
21+
}
22+
end++;
23+
maxCount = Math.max(maxCount, count);
24+
if (vowels.indexOf(s.charAt(start)) != -1) {
25+
map.put(s.charAt(start), map.getOrDefault(s.charAt(start), 0) - 1);
26+
count--;
27+
}
28+
start++;
29+
}
30+
return maxCount;
31+
}
32+
}

0 commit comments

Comments
 (0)