forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountVowelSubstringsOfAString.java
51 lines (45 loc) · 1.76 KB
/
CountVowelSubstringsOfAString.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// https://leetcode.com/problems/count-vowel-substrings-of-a-string
// T: O(|word|)
// S: O(|word|)
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class CountVowelSubstringsOfAString {
private static final Set<Character> VOWELS = Set.of('a', 'e', 'i', 'o','u');
public static int countVowelSubstrings(String word) {
int vowelSubstrings = 0;
final Map<Character, Integer> vowels = new HashMap<>();
for (int j = 0 ; j < word.length() ; j++) {
if (isVowel(word.charAt(j))) {
addToMap(vowels, word.charAt(j));
for (int i = j + 1, k = j ; i < word.length() ; i++) {
if (isVowel(word.charAt(i))) {
addToMap(vowels, word.charAt(i));
for ( ; k < i && vowels.size() == 5 ; k++) {
removeFromMap(vowels, word.charAt(k));
}
vowelSubstrings += k - j;
if (i == word.length() - 1) {
j = i;
}
} else {
j = i;
vowels.clear();
break;
}
}
}
}
return vowelSubstrings;
}
private static boolean isVowel(char c) {
return VOWELS.contains(c);
}
private static void addToMap(Map<Character, Integer> frequencies, char c) {
frequencies.put(c, frequencies.getOrDefault(c, 0) + 1);
}
private static void removeFromMap(Map<Character, Integer> frequencies, char c) {
if (frequencies.getOrDefault(c, 0) == 1) frequencies.remove(c);
else frequencies.put(c, frequencies.get(c) - 1);
}
}