Longest Substring Of All Vowels in Order - Problem
A string is considered beautiful if it satisfies the following conditions:
- Each of the 5 English vowels (
'a','e','i','o','u') must appear at least once in it. - The letters must be sorted in alphabetical order (i.e. all
'a's before'e's, all'e's before'i's, etc.).
For example, strings "aeiou" and "aaaaaaeiiiioou" are considered beautiful, but "uaeio", "aeoiu", and "aaaeeeooo" are not beautiful.
Given a string word consisting of English vowels, return the length of the longest beautiful substring of word. If no such substring exists, return 0.
A substring is a contiguous sequence of characters in a string.
Input & Output
Example 1 — Basic Beautiful Substring
$
Input:
word = "aeiouaaaeiou"
›
Output:
13
💡 Note:
The longest beautiful substring is "aeiouaaaeiou" with length 13. It contains all vowels a,e,i,o,u in order.
Example 2 — Multiple Beautiful Substrings
$
Input:
word = "aeiou"
›
Output:
5
💡 Note:
The entire string "aeiou" is beautiful with length 5. It contains each vowel exactly once in alphabetical order.
Example 3 — No Beautiful Substring
$
Input:
word = "uaeio"
›
Output:
0
💡 Note:
No beautiful substring exists because the vowels are not in alphabetical order.
Constraints
- 1 ≤ word.length ≤ 5 × 104
- word consists of only English vowels
Visualization
Tap to expand
Understanding the Visualization
1
Input
String with vowels: "aeiouaaaeiou"
2
Process
Find substrings with all 5 vowels in alphabetical order
3
Output
Length of longest beautiful substring: 13
Key Takeaway
🎯 Key Insight: Track vowel sequence progress and find longest complete beautiful substring
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code