Count Vowel Substrings of a String - Problem
Imagine you're a linguist studying complete vowel patterns in words! Your task is to find all the special substrings that contain every single vowel in the English language.
A vowel substring is a contiguous sequence of characters that:
- Contains only vowels ('a', 'e', 'i', 'o', 'u')
- Has all five vowels present at least once
Given a string word, return the count of such magical vowel substrings.
Example: In "aeiouu", the substring "aeiou" qualifies because it contains only vowels and has all five different vowels!
Input & Output
example_1.py โ Basic Case
$
Input:
word = "aeiouu"
โบ
Output:
2
๐ก Note:
The vowel substrings are "aeiou" and "aeiouu". Both contain only vowels and have all five vowels present.
example_2.py โ With Consonants
$
Input:
word = "unicornarihan"
โบ
Output:
0
๐ก Note:
Although there are vowels, no substring contains only vowels AND has all five vowels present. The consonants break up the vowel sequences.
example_3.py โ Complex Pattern
$
Input:
word = "cuaeiouueioa"
โบ
Output:
7
๐ก Note:
Multiple valid substrings exist within the vowel-only section "aeiouueioa". Each must contain all five vowels: aeiou, aeiouu, aeiouue, aeiouuei, aeiouueio, aeiouueioa, uueioa.
Visualization
Tap to expand
Understanding the Visualization
1
Skip the Noise
Like a detective ignoring irrelevant clues, skip any consonants since they can't be part of our target
2
Start Collecting Evidence
When you find a vowel, start your evidence collection - track each unique vowel type you encounter
3
Build Complete Cases
Once you have all 5 evidence types, you've solved one case! Every extension still gives you another complete case
4
Break on Contamination
If you hit a consonant, your evidence chain is broken - start fresh from the next vowel
Key Takeaway
๐ฏ Key Insight: The detective approach (optimized sliding window) is like having a smart evidence collection strategy - skip the noise, track unique evidence types, and count complete cases as you build them. This gives us O(n) efficiency instead of checking every possible evidence combination!
Time & Space Complexity
Time Complexity
O(n)
Single pass through string with constant work per character
โ Linear Growth
Space Complexity
O(1)
Only using fixed-size hash map for 5 vowels plus a few variables
โ Linear Space
Constraints
- 1 โค word.length โค 100
- word consists of lowercase English letters only
- All five vowels (a, e, i, o, u) must be present in a valid substring
- Valid substrings must contain only vowel characters
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code