Skip to content

Commit cca4b89

Browse files
authored
Create Count Vowel Substrings of a String.java
1 parent d180b80 commit cca4b89

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 countVowelSubstrings(String word) {
3+
return atMostVowel(word, 5) - atMostVowel(word, 4);
4+
}
5+
6+
private int atMostVowel(String word, int atMostGoal) {
7+
int start = 0;
8+
int count = 0;
9+
int n = word.length();
10+
Map<Character, Integer> map = new HashMap<>();
11+
for (int end = 0; end < n; end++) {
12+
if (!isVowel(word.charAt(end))) {
13+
map.clear();
14+
start = end + 1;
15+
} else {
16+
map.put(word.charAt(end), map.getOrDefault(word.charAt(end), 0) + 1);
17+
for (; map.size() > atMostGoal; start++) {
18+
map.put(word.charAt(start), map.get(word.charAt(start)) - 1);
19+
if (map.get(word.charAt(start)) == 0) {
20+
map.remove(word.charAt(start));
21+
}
22+
}
23+
count += end - start + 1;
24+
}
25+
}
26+
return count;
27+
}
28+
29+
private boolean isVowel(char c) {
30+
return "aeiou".indexOf(c) != -1;
31+
}
32+
}

0 commit comments

Comments
 (0)