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
🎼 Musical Scale: Finding Beautiful Vowel SequencesAEIOUMust progress in this order →Example Performance Analysis:Input: "aaeeeiiiioooouuuu" ✅ Perfect performance - all notes in orderInput: "aaeeiouuaa" ❌ Singer went back to 'A' after 'U'Input: "aaeeiiiooouuuaeiou" ✅ Two performances, longest is "aeiou"Algorithm Steps:1. 🎵 Find starting note 'A' (beautiful sequences must start with 'a')2. 🎶 Track current note and allow repetition (aaaa → eeee → etc.)3. ✅ Record length when all 5 notes completed4. 🔄 Reset and start new sequence on wrong note
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

n
2n
Linear Growth
Space Complexity
O(1)

Only using a few variables to track window state

n
2n
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
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
67.4K Views
Medium-High Frequency
~18 min Avg. Time
1.9K 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