Count of Substrings Containing Every Vowel and K Consonants I - Problem
Given a string word and a non-negative integer k, you need to find the total number of substrings that satisfy two conditions:
- The substring contains every vowel (
'a','e','i','o', and'u') at least once - The substring contains exactly
kconsonants
A substring is a contiguous sequence of characters within a string. Vowels are the letters 'a', 'e', 'i', 'o', and 'u'. All other lowercase English letters are consonants.
Example: For word = "aeiouk" and k = 1, the substring "aeiouk" contains all vowels and exactly 1 consonant ('k'), so it counts toward our answer.
Input & Output
example_1.py โ Basic Case
$
Input:
word = "aeioqq", k = 1
โบ
Output:
0
๐ก Note:
No substring contains all vowels (a,e,i,o,u) and exactly 1 consonant. The string is missing 'u' vowel.
example_2.py โ Valid Substring
$
Input:
word = "aeiouqq", k = 1
โบ
Output:
1
๐ก Note:
The substring "aeiouq" (indices 0-5) contains all vowels and exactly 1 consonant 'q'.
example_3.py โ Multiple Valid Substrings
$
Input:
word = "aeioubac", k = 2
โบ
Output:
1
๐ก Note:
The substring "aeioubac" contains all vowels (a,e,i,o,u) and exactly 2 consonants (b,c).
Constraints
- 5 โค word.length โค 5 ร 104
- word consists only of lowercase English letters
- 0 โค k โค word.length - 5
- Note: Since we need all 5 vowels, minimum substring length is 5
Visualization
Tap to expand
Understanding the Visualization
1
Initialize Window
Start with an empty window at the beginning of the string
2
Expand Right
Extend the window to the right, counting vowels and consonants
3
Check Consonants
If consonants exceed k, shrink window from the left
4
Count Valid
When we have all vowels and โคk consonants, count valid substrings
5
Use Difference
Use 'at most k' - 'at most k-1' to get exactly k consonants
Key Takeaway
๐ฏ Key Insight: Using the mathematical relationship (exactly k) = (at most k) - (at most k-1) allows us to solve this efficiently with sliding window technique in O(n) time.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code