Count of Substrings Containing Every Vowel and K Consonants II - Problem

You're tasked with finding all perfect substrings in a given word. A perfect substring is one that contains every vowel (a, e, i, o, u) at least once and has exactly k consonants.

Given a string word and a non-negative integer k, return the total count of such perfect substrings.

Example: For word = "aeioqq" and k = 2, the substring "aeioqq" contains all vowels (a, e, i, o) and exactly 2 consonants (q, q), making it a perfect substring.

This problem tests your ability to efficiently track character frequencies while exploring all possible substring combinations using advanced string manipulation techniques.

Input & Output

example_1.py โ€” Basic Case
$ Input: word = "aeioqq", k = 2
โ€บ Output: 1
๐Ÿ’ก Note: The entire string "aeioqq" contains vowels a,e,i,o (missing u) and 2 consonants (q,q). Since it's missing vowel 'u', it's not valid. However, the substring "aeioqu" would be valid if 'u' was present.
example_2.py โ€” Perfect Match
$ Input: word = "aeiou", k = 0
โ€บ Output: 1
๐Ÿ’ก Note: The string "aeiou" contains all 5 vowels exactly once and 0 consonants. This matches our requirement of k=0 consonants, so there's exactly 1 valid substring.
example_3.py โ€” Multiple Valid Substrings
$ Input: word = "aeioubcaeiou", k = 2
โ€บ Output: 3
๐Ÿ’ก Note: Multiple substrings contain all vowels and exactly 2 consonants: "aeioubcae", "eioubcaei", "ioubcaeio". Each sliding window position creates valid substrings.

Visualization

Tap to expand
Sliding Window Algorithm VisualizationString: "aeioubcaeiou"aeioubcSliding Window (k=2)Current Window State:Vowels Found: aโœ“ eโœ“ iโœ“ oโœ“ uโœ“Consonants: 2 (b, c)โœ“ Valid: All vowels + exactly 2 consonantsValid substrings from this position: 3Algorithm Steps:1. Expand window by moving right pointer2. Update vowel and consonant counts3. If consonants > k, shrink from left4. If all vowels present, count valid substrings5. Result = atMost(k) - atMost(k-1)Time: O(n) | Space: O(1)Key Mathematical Insight:exactly(k) = atMost(k) - atMost(k-1)This transforms "exactly k consonants" into two simpler "at most" problems
Understanding the Visualization
1
Initialize Window
Start with empty counters for vowels and consonants
2
Expand Window
Move right pointer, updating character counts
3
Shrink When Needed
If consonants exceed limit, shrink from left
4
Count Valid Windows
When all vowels present, count valid substrings
5
Apply Math Transform
Use atMost(k) - atMost(k-1) = exactly(k)
Key Takeaway
๐ŸŽฏ Key Insight: The sliding window with at-most transformation efficiently solves the exact counting problem by leveraging the mathematical relationship: exactly(k) = atMost(k) - atMost(k-1)

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each character is visited at most twice (once by right pointer, once by left pointer)

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for vowel and consonant counters

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค word.length โ‰ค 105
  • 0 โ‰ค k โ‰ค word.length
  • word consists only of lowercase English letters
  • All vowels must be present at least once
  • Exactly k consonants are required
Asked in
Google 45 Microsoft 38 Amazon 32 Meta 28
23.8K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen