Similar Problems

Similar Problems not available

Longest Substring Of All Vowels In Order - Leetcode Solution

Companies:

LeetCode:  Longest Substring Of All Vowels In Order Leetcode Solution

Difficulty: Medium

Topics: string sliding-window  

The Longest Substring Of All Vowels In Order problem on LeetCode requires finding the longest substring in a string that contains all vowels in alphabetical order. The vowels are defined as 'a', 'e', 'i', 'o', and 'u'.

To solve this problem, we need to keep track of the current vowel we are looking for and also the current length of the substring we are considering. We can start by initializing the current vowel to 'a' and the current length to zero.

Then, we can iterate through the string character by character and check if the current character is the same as the current vowel we are looking for. If it is, we increment the current length and move on to the next vowel. If it isn't, we reset the current length to zero and start looking for 'a' again.

We also need to keep track of the length of the longest substring we have found so far and update it every time we find a longer substring.

Here's the code to implement this algorithm:

class Solution:
    def longestBeautifulSubstring(self, word: str) -> int:
        vowels = 'aeiou'
        current_vowel = 'a'  # start with 'a'
        current_length = 0
        longest_length = 0
        
        for c in word:
            if c == current_vowel:
                current_length += 1
                if current_vowel == 'u':  # reached end of substring
                    longest_length = max(longest_length, current_length)
            elif c in vowels:
                if current_vowel == 'u':  # found a new substring
                    current_vowel = 'a'
                    current_length = 1
                elif c == current_vowel:  # skip repeated vowels
                    continue
                else:  # reset substring
                    current_vowel = 'a'
                    current_length = 0
            else:  # not a vowel, reset substring
                current_vowel = 'a'
                current_length = 0
                
        return longest_length

This solution has a time complexity of O(n), where n is the length of the input string, because we only do a single pass through the string. The space complexity is O(1) because we only need a few variables to keep track of our progress through the string.

Longest Substring Of All Vowels In Order Solution Code

1