Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Longest Substring Of All Vowels in Order

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1839" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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'.

Explanation

Here's the breakdown of the approach, complexities, and the Python code:

  • High-Level Approach:

    • Iterate through the string, identifying potential starting points of beautiful substrings ('a').
    • For each potential start, extend the substring as long as the vowels are in the correct order and the vowels 'a','e','i','o','u' appear at least once.
    • Keep track of the longest beautiful substring found so far.
  • Complexity:

    • Runtime Complexity: O(n) - We iterate through the string at most twice in the worst case.
    • Storage Complexity: O(1) - We use a fixed number of variables.

Code

    def longestBeautifulSubstring(word: str) -> int:
    """
    Finds the length of the longest beautiful substring of a word.
    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.
    """
    n = len(word)
    if n < 5:
        return 0

    max_len = 0
    i = 0
    while i < n:
        if word[i] == 'a':
            j = i
            vowels = set()
            while j < n:
                vowels.add(word[j])
                if j + 1 < n and word[j + 1] < word[j]:
                    break
                j += 1

            if len(vowels) == 5:
                max_len = max(max_len, j - i + 1)
            i = j
        else:
            i += 1

    return max_len

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Longest Substring Of All Vowels in Order