Skip to content

Commit 1a402e0

Browse files
committed
Add solution #3306
1 parent 1e7bfd0 commit 1a402e0

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@
836836
3174|[Clear Digits](./3174-clear-digits.js)|Easy|
837837
3208|[Alternating Groups II](./3208-alternating-groups-ii.js)|Medium|
838838
3223|[Minimum Length of String After Operations](./3223-minimum-length-of-string-after-operations.js)|Medium|
839+
3306|[Count of Substrings Containing Every Vowel and K Consonants II](./3306-count-of-substrings-containing-every-vowel-and-k-consonants-ii.js)|Medium|
839840
3392|[Count Subarrays of Length Three With a Condition](./3392-count-subarrays-of-length-three-with-a-condition.js)|Easy|
840841
3396|[Minimum Number of Operations to Make Elements in Array Distinct](./3396-minimum-number-of-operations-to-make-elements-in-array-distinct.js)|Easy|
841842
3397|[Maximum Number of Distinct Elements After Operations](./3397-maximum-number-of-distinct-elements-after-operations.js)|Medium|
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* 3306. Count of Substrings Containing Every Vowel and K Consonants II
3+
* https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string word and a non-negative integer k.
7+
*
8+
* Return the total number of substrings of word that contain every vowel ('a', 'e', 'i', 'o', and
9+
* 'u') at least once and exactly k consonants.
10+
*/
11+
12+
/**
13+
* @param {string} word
14+
* @param {number} k
15+
* @return {number}
16+
*/
17+
function countOfSubstrings(word, k) {
18+
return helper(word, k) - helper(word, k - 1);
19+
20+
function helper(word, k) {
21+
const vowels = { a: 0, e: 0, i: 0, o: 0, u: 0 };
22+
let count = 0;
23+
let left = 0;
24+
let consonants = k;
25+
26+
for (let right = 0; right < word.length; right++) {
27+
const character = word[right];
28+
if (character in vowels) {
29+
vowels[character]++;
30+
} else {
31+
consonants--;
32+
}
33+
34+
while (vowels.a > 0 && vowels.e > 0 && vowels.i > 0 && vowels.o > 0
35+
&& vowels.u > 0 && consonants < 0) {
36+
const leftCharacter = word[left];
37+
if (leftCharacter in vowels) {
38+
vowels[leftCharacter]--;
39+
} else {
40+
consonants++;
41+
}
42+
left++;
43+
}
44+
count += right - left + 1;
45+
}
46+
return count;
47+
}
48+
}

0 commit comments

Comments
 (0)