3305. Count of Substrings Containing Every Vowel and K Consonants I
SourceWeekly Contest 417 Q2DifficultyMediumRating1563
Description
You are given a string word and a non-negative integer k.
Return the total number of substrings of word that contain every vowel ('a', 'e', 'i', 'o', and 'u') at least once and exactly k consonants.
Example 1:
Input: word = "aeioqq", k = 1
Output: 0
Explanation:
There is no substring with every vowel.
Example 2:
Input: word = "aeiou", k = 0
Output: 1
Explanation:
The only substring with every vowel and zero consonants is word[0..4], which is "aeiou".
Example 3:
Input: word = "ieaouqqieaouqq", k = 1
Output: 3
Explanation:
The substrings with every vowel and one consonant are:
word[0..5], which is"ieaouq".word[6..11], which is"qieaou".word[7..12], which is"ieaouq".
Constraints:
5 <= word.length <= 250wordconsists only of lowercase English letters.0 <= k <= word.length - 5
Solutions
Solution 1: Problem Transformation + Sliding Window
Thinking
With \(|\textit{word}| \le 250\), enumerating substrings works, but a window that must contain every vowel and exactly \(k\) consonants is not monotone in a single constraint.
We rewrite “exactly \(k\) consonants” as \(f(k)-f(k+1)\), where \(f(t)\) counts substrings with all five vowels and at least \(t\) consonants. The “at least” form lets the left pointer advance whenever the window is valid.
Every substring ending at the current right end and starting in \([0, l)\) is then valid, so we add \(l\). A map tracks the five vowels; consonants use a separate counter \(x\).
We can transform the problem into solving the following two subproblems:
- Find the total number of substrings where each vowel appears at least once and contains at least \(k\) consonants, denoted as \(\textit{f}(k)\);
- Find the total number of substrings where each vowel appears at least once and contains at least \(k + 1\) consonants, denoted as \(\textit{f}(k + 1)\).
Then the answer is \(\textit{f}(k) - \textit{f}(k + 1)\).
Therefore, we design a function \(\textit{f}(k)\) to count the total number of substrings where each vowel appears at least once and contains at least \(k\) consonants.
We can use a hash table \(\textit{cnt}\) to count the occurrences of each vowel, a variable \(\textit{ans}\) to store the answer, a variable \(\textit{l}\) to record the left boundary of the sliding window, and a variable \(\textit{x}\) to record the number of consonants in the current window.
Traverse the string. If the current character is a vowel, add it to the hash table \(\textit{cnt}\); otherwise, increment \(\textit{x}\) by one. If \(\textit{x} \ge k\) and the size of the hash table \(\textit{cnt}\) is \(5\), it means the current window meets the conditions. We then move the left boundary in a loop until the window no longer meets the conditions. At this point, all substrings ending at the right boundary \(\textit{r}\) and with the left boundary in the range \([0, .. \textit{l} - 1]\) meet the conditions, totaling \(\textit{l}\) substrings. We add \(\textit{l}\) to the answer. Continue traversing the string until the end, and we get \(\textit{f}(k)\).
Finally, we return \(\textit{f}(k) - \textit{f}(k + 1)\).
The time complexity is \(O(n)\), where \(n\) is the length of the string \(\textit{word}\). The space complexity is \(O(1)\).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
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 | |
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 | |
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 | |
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 | |
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 | |