Skip to main content

Longest Substring Of All Vowels in Order - Solution & Explanation

MediumStringSliding Window19 min readAsked at: Microsoft, Salesforce, PayPal +2
Practice this problem

Problem Statement

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.

 

Example 1:

Input: word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
Output: 13
Explanation: The longest beautiful substring in word is "aaaaeiiiiouuu" of length 13.

Example 2:

Input: word = "aeeeiiiioooauuuaeiou"
Output: 5
Explanation: The longest beautiful substring in word is "aeiou" of length 5.

Example 3:

Input: word = "a"
Output: 0
Explanation: There is no beautiful substring, so return 0.

 

Constraints:

  • 1 <= word.length <= 5 * 105
  • word consists of characters 'a', 'e', 'i', 'o', and 'u'.

Approach Overview

Problem Overview: You receive a string and need the length of the longest substring where vowels appear in strictly non-decreasing alphabetical order: a → e → i → o → u. The substring must contain all five vowels at least once and maintain this order throughout.

Approach 1: Sliding Window Scan (O(n) time, O(1) space)

This problem fits naturally with a sliding window style scan. Iterate through the string once while tracking the current valid vowel sequence. If the current character maintains alphabetical order relative to the previous one, extend the window. If the order breaks (for example i → a), reset the window starting from the current character. Maintain a counter for how many distinct vowels have appeared in sequence. Whenever the sequence includes all five vowels, update the maximum substring length. This works because a valid substring is always contiguous and the vowel ordering constraint only depends on adjacent characters.

Approach 2: Dynamic Programming with Vowel Stages (O(n) time, O(1) space)

The problem can also be modeled as stages of vowel progression using dynamic programming. Track the longest valid sequence ending at each vowel stage (a, e, i, o, u). When processing a character, update the stage corresponding to that vowel: continuing the same stage if repeated or transitioning from the previous stage if the order advances. For example, an e can extend an existing e sequence or transition from an a sequence. Once the u stage grows, update the answer. This method formalizes the ordering constraint but requires more bookkeeping than the sliding window.

Both methods rely heavily on sequential character processing, which is typical for string problems where order matters but backtracking is unnecessary.

Recommended for interviews: The sliding window approach is usually what interviewers expect. It demonstrates that you recognize the ordered constraint and can track contiguous segments efficiently in a single pass. Mentioning the dynamic programming variant shows deeper algorithmic thinking, but the O(n) sliding scan is simpler and easier to implement under time pressure.

Approach 1: Sliding Window Approach

The sliding window approach can be used to efficiently find the longest substring that contains all vowels in order.

  1. Initialize two pointers to maintain the sliding window boundaries.
  2. Iterate over the string with these two pointers to identify valid substrings.
  3. Keep track of encountered vowels and ensure they appear in the correct order.
  4. When the sequence breaks, adjust the window accordingly.

The C implementation uses a loop that iterates through the given string with an array of vowels to verify that all vowels are present in order within the substring. As each vowel is encountered consecutively in order, the length of the substring increments. If the substring is beautiful, it updates the maximum length.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)

Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Dynamic Programming Approach

Utilizing dynamic programming can also assist in achieving an efficient solution to identify the longest beautiful substring. The premise involves storing intermediate results to make decisions on forming a valid substring based on past computations.

This method, however, can be more complex and may require additional data structures compared to the sliding window approach.

This C implementation uses a 2D array to store results for each character in the input string, at each step comparing the progress towards completing a beautiful substring. It keeps track of these to determine the longest length possible.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)

Space Complexity: O(n)

Try this approach in the editor →

Approach 3: Two Pointers + Simulation

We can first transform the string word. For example, for word="aaaeiouu", we can transform it into data items ('a', 3), ('e', 1), ('i', 1), ('o', 1), ('u', 2) and store them in an array arr. Each data item's first element represents a vowel, and the second element represents the number of times the vowel appears consecutively. This transformation can be implemented using two pointers.

Next, we traverse the array arr, each time taking 5 adjacent data items, and judge whether the vowels in these data items are 'a', 'e', 'i', 'o', 'u' respectively. If so, calculate the total number of times the vowels appear in these 5 data items, which is the length of the current beautiful substring, and update the maximum value of the answer.

The time complexity is O(n), and the space complexity is O(n). Where n is the length of the string word.

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Sliding Window Approach

Time Complexity: O(n)

Space Complexity: O(1)

Dynamic Programming Approach

Time Complexity: O(n)

Space Complexity: O(n)

Two Pointers + Simulation

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Sliding Window ScanO(n)O(1)Best general solution. Single pass with minimal state tracking.
Dynamic Programming (Vowel Stages)O(n)O(1)Useful when modeling ordered transitions between character groups.

Video Solution

Longest Substring Of All Vowels in OrderNalin Goyal1,765 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Longest Substring Of All Vowels in Order easy or hard?
The problem is rated Medium because recognizing the ordered vowel constraint and maintaining a valid substring requires careful logic. Once the sliding window idea is identified, the implementation becomes straightforward with linear time complexity.
Longest Substring Of All Vowels in Order Python/Java solution
Both Python and Java implementations follow the same logic: iterate through the string, compare each character with the previous one, and maintain the length of the current valid substring. When the substring contains all five vowels in order, update the maximum length.
How to solve Longest Substring Of All Vowels in Order in O(n)?
Traverse the string once while comparing each character with the previous one. If the vowel order stays the same or increases alphabetically, extend the current substring; otherwise reset the start. Track how many unique vowels have appeared, and when all five are present update the maximum length.
What is the best approach for Longest Substring Of All Vowels in Order?
The sliding window approach is the most efficient and commonly expected solution. It scans the string once, extending a window while vowels remain in non-decreasing alphabetical order and resetting when the order breaks. This achieves O(n) time and O(1) space.
Is Longest Substring Of All Vowels in Order asked at Google/Amazon/Meta?
This problem represents a common pattern used in interviews at large tech companies. Variations involving ordered substrings, vowel sequences, or monotonic character constraints frequently appear in interviews at companies like Amazon, Google, and Meta.
What data structure is used in Longest Substring Of All Vowels in Order?
The solution primarily uses simple variables and counters rather than complex data structures. The key technique is a sliding window over the string combined with tracking vowel transitions such as a→e→i→o→u.
What is the time complexity of Longest Substring Of All Vowels in Order?
The optimal solution runs in O(n) time where n is the length of the string. Each character is processed once during a linear scan. Space complexity is O(1) because only a few counters and the previous character need to be tracked.

Ready to solve this problem?

Practice Longest Substring Of All Vowels in Order with our built-in code editor and test cases.

Practice on FleetCode