Skip to content

Commit af700c6

Browse files
authored
Create 3306. Count of Substrings Containing Every Vowel and K Consona… (#736)
2 parents 5fa5dea + 0c546dc commit af700c6

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
bool isVowel(char c) {
4+
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
5+
}
6+
7+
long long atLeastK(string& word, int k) {
8+
int n = word.size();
9+
long long ans = 0;
10+
int consonants = 0;
11+
int left = 0;
12+
unordered_map<char, int> vowel_map;
13+
14+
for (int right = 0; right < n; right++) {
15+
if (isVowel(word[right])) {
16+
vowel_map[word[right]]++;
17+
} else {
18+
consonants++;
19+
}
20+
21+
while (vowel_map.size() == 5 && consonants >= k) {
22+
ans += n - right;
23+
if (isVowel(word[left])) {
24+
vowel_map[word[left]]--;
25+
if (vowel_map[word[left]] == 0) {
26+
vowel_map.erase(word[left]);
27+
}
28+
} else {
29+
consonants--;
30+
}
31+
left++;
32+
}
33+
}
34+
return ans;
35+
}
36+
37+
long long countOfSubstrings(string word, int k) {
38+
return atLeastK(word, k) - atLeastK(word, k + 1);
39+
}
40+
};

0 commit comments

Comments
 (0)