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

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.

Input & Output

Example 1 — Basic Case
$ Input: word = "aeioubcdfghiju", k = 1
Output: 0
💡 Note: No substring contains all vowels and exactly 1 consonant. Need at least 5 characters for all vowels, but with 1 consonant we'd need 6 total characters minimum.
Example 2 — All Vowels Present
$ Input: word = "aeiou", k = 0
Output: 1
💡 Note: The substring "aeiou" contains all 5 vowels and exactly 0 consonants, so count is 1.
Example 3 — Multiple Valid Substrings
$ Input: word = "aeioubcd", k = 3
Output: 1
💡 Note: The substring "aeioubcd" contains all vowels (a,e,i,o,u) and exactly 3 consonants (b,c,d).

Constraints

  • 1 ≤ word.length ≤ 105
  • word consists of only lowercase English letters
  • 0 ≤ k ≤ word.length

Visualization

Tap to expand
Count Substrings with Every Vowel and K ConsonantsInput: word = "aeiou" k = 0aeiouAll 5 vowels present, 0 consonantsValid substring: "aeiou"Requirements:✓ Contains all vowels: a, e, i, o, u✓ Exactly 0 consonantsOutput: 1 valid substring
Understanding the Visualization
1
Input
String with vowels and consonants, target consonant count
2
Process
Use sliding window to find valid substrings
3
Output
Count of substrings with all vowels and exactly k consonants
Key Takeaway
🎯 Key Insight: Transform exact count to difference of at-most counts using sliding window
Asked in
Google 25 Microsoft 20 Amazon 15 Meta 10
28.0K Views
Medium Frequency
~25 min Avg. Time
850 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