Count of Substrings Containing Every Vowel and K Consonants I - 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 = "aeioqq", k = 1
Output: 0
💡 Note: No substring contains all vowels (a,e,i,o,u) and exactly 1 consonant. Missing vowel 'u'.
Example 2 — Valid Case
$ Input: word = "aeiou", k = 0
Output: 1
💡 Note: The substring "aeiou" contains all vowels and exactly 0 consonants.
Example 3 — Multiple Valid
$ Input: word = "aeiouc", k = 1
Output: 1
💡 Note: The substring "aeiouc" contains all vowels (a,e,i,o,u) and exactly 1 consonant (c).

Constraints

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

Visualization

Tap to expand
Substrings with All Vowels + K Consonants INPUT word = "aeioqq" a vowel e vowel i vowel o vowel q cons q cons Parameters k = 1 Need exactly 1 consonant Required Vowels a, e, i, o, u All 5 must appear at least once ALGORITHM STEPS 1 Enumerate Substrings Check all possible substrings 2 Count Vowels Track a,e,i,o,u presence 3 Count Consonants Must equal exactly k 4 Validate All vowels + k consonants? Analysis for "aeioqq" - Has vowels: a,e,i,o - Missing vowel: u - Has 2 consonants (q,q) - No valid substring exists! FINAL RESULT Output: 0 Why 0? String has only 4 vowels: a, e, i, o Missing vowel 'u' means no substring can contain ALL 5 required vowels! No Valid Substrings Condition cannot be satisfied count = 0 Key Insight: For a valid substring, we need ALL 5 vowels (a,e,i,o,u) present at least once AND exactly k consonants. If the input string is missing ANY vowel, no substring can satisfy the condition --> result is always 0. TutorialsPoint - Count of Substrings Containing Every Vowel and K Consonants I | Optimal Solution
Asked in
Google 15 Microsoft 12 Amazon 10
8.5K Views
Medium Frequency
~25 min Avg. Time
340 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