Magicsheet logo

Longest Substring Of All Vowels in Order

Medium
100%
Updated 6/1/2025

Longest Substring Of All Vowels in Order

What is this problem about?

The Longest Substring Of All Vowels in Order problem provides a string consisting entirely of lowercase English vowels ('a', 'e', 'i', 'o', 'u'). Your objective is to find the length of the longest contiguous substring where all five vowels are present, and they appear in strict alphabetical order. For example, "aaeeiouu" is valid, but "aaeioua" is not because 'a' appears after 'u'.

Why is this asked in interviews?

This is a great string parsing and sliding window problem. Interviewers ask it to test your ability to manage state transitions. It requires tracking multiple conditions at once: ensuring characters are in alphabetical order, counting the sequence length, and verifying that all five unique vowels have been seen before validating a sequence. It perfectly evaluates clean, structured conditional logic.

Algorithmic pattern used

This problem uses a Single Pass / Sliding Window pattern. As you iterate through the string, you track the length of the current alphabetical sequence and the number of unique vowels seen in this sequence. If the current character is greater than or equal to the previous character (e.g., 'e' after 'a', or 'e' after 'e'), you increment the length. If it is strictly greater, you also increment the unique vowel count. If the sequence breaks, you reset your state.

Example explanation

Consider the string "aeiaaioooaauuaeiu". Let's analyze the substring "aaioooaauu": It breaks order at the second 'a'. Let's analyze the substring "aeiou": It's valid, length 5. What about "aaeiouu"?

  • 'a' -> 'a' (Valid, Length 2, Unique 1)
  • 'a' -> 'e' (Valid, Length 3, Unique 2)
  • 'e' -> 'i' (Valid, Length 4, Unique 3)
  • 'i' -> 'o' (Valid, Length 5, Unique 4)
  • 'o' -> 'u' (Valid, Length 6, Unique 5)
  • 'u' -> 'u' (Valid, Length 7, Unique 5) Since Unique == 5, this is a valid beautiful substring of length 7! If the next char was 'a', the sequence breaks, and we'd reset length to 1.

Common mistakes candidates make

A major pitfall is validating strings that are in order but missing vowels. For example, "aaeeoo" is in alphabetical order, but it lacks 'i' and 'u'. Candidates sometimes return the length of this string, forgetting the strict requirement that all five vowels must be present. Another mistake is poorly handling the reset logic when the alphabetical sequence breaks.

Interview preparation tip

When tackling the Longest Substring Of All Vowels in Order interview pattern, assign integer values to vowels mentally (a=1, e=2, i=3, o=4, u=5). This makes the "in order" check a simple \ge comparison. Ensure your reset logic specifically checks if the new starting character is an 'a', because a valid sequence must begin with 'a'.

Similar Questions