Longest Substring Of All Vowels in Order - Problem
Imagine you're a linguist searching for the most beautiful vowel patterns in ancient texts! Your task is to find the longest substring that contains all 5 English vowels in perfect alphabetical order.
A substring is considered beautiful if it meets these criteria:
- Contains all 5 vowels:
'a','e','i','o','u'at least once - Vowels appear in strict alphabetical order (all
'a's before any'e', all'e's before any'i', etc.)
Examples:
- ✅
"aeiou"- Perfect order, all vowels present - ✅
"aaaaeeeiiiooouu"- Multiple occurrences in order - ❌
"uaeio"- Wrong order (u comes before a) - ❌
"aaaeeeooo"- Missing vowels (i, u)
Given a string containing only English vowels, return the length of the longest beautiful substring. If no beautiful substring exists, return 0.
Input & Output
example_1.py — Python
$
Input:
word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
›
Output:
13
💡 Note:
The longest beautiful substring is "aaaaeiiiiouuu" which has length 13. It contains all vowels (a,e,i,o,u) in alphabetical order.
example_2.py — Python
$
Input:
word = "aeeeiiiioooauuuaeiou"
›
Output:
5
💡 Note:
The longest beautiful substring is "aeiou" at the end, which has length 5. The first part "aeeeiiiioooauuu" breaks the order when 'a' appears after 'o'.
example_3.py — Python
$
Input:
word = "a"
›
Output:
0
💡 Note:
A single vowel cannot form a beautiful substring since we need all 5 vowels. Return 0.
Visualization
Tap to expand
Understanding the Visualization
1
Start the Scale
Every beautiful performance must begin with note 'A' - scan until we find a starting point
2
Progress Through Notes
Allow the singer to hold each note (repeat vowels) but advance to next note when ready
3
Track Completion
Monitor which notes have been sung - we need all 5 for a complete performance
4
Reset on Wrong Note
If singer goes backwards or skips ahead incorrectly, start looking for a new performance
Key Takeaway
🎯 Key Insight: Use sliding window with state tracking - every beautiful sequence starts with 'a' and progresses through vowels in order. Reset the window whenever the musical scale breaks!
Time & Space Complexity
Time Complexity
O(n)
Single pass through the string with constant work per character
✓ Linear Growth
Space Complexity
O(1)
Only using a few variables to track window state
✓ Linear Space
Constraints
- 1 ≤ word.length ≤ 5 × 105
- word consists of vowels only: 'a', 'e', 'i', 'o', and 'u'
- Beautiful substring requires all 5 vowels in alphabetical order
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code