1839. Longest Substring Of All Vowels in Order LeetCode Solution

In this guide, you will get 1839. Longest Substring Of All Vowels in Order LeetCode Solution with the best time and space complexity. The solution to Longest Substring Of All Vowels in Order problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Longest Substring Of All Vowels in Order solution in C++
  4. Longest Substring Of All Vowels in Order solution in Java
  5. Longest Substring Of All Vowels in Order solution in Python
  6. Additional Resources
1839. Longest Substring Of All Vowels in Order LeetCode Solution image

Problem Statement of Longest Substring Of All Vowels in Order

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.

See also  546. Remove Boxes LeetCode Solution

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1839. Longest Substring Of All Vowels in Order LeetCode Solution in C++

class Solution {
 public:
  int longestBeautifulSubstring(string word) {
    int ans = 0;
    int count = 1;

    for (int l = 0, r = 1; r < word.length(); ++r) {
      const char curr = word[r];
      const char prev = word[r - 1];
      if (curr >= prev) {
        if (curr > prev)
          ++count;
        if (count == 5)
          ans = max(ans, r - l + 1);
      } else {
        count = 1;
        l = r;
      }
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

1839. Longest Substring Of All Vowels in Order LeetCode Solution in Java

class Solution {
  public int longestBeautifulSubstring(String word) {
    int ans = 0;
    int count = 1;

    for (int l = 0, r = 1; r < word.length(); ++r) {
      final char curr = word.charAt(r);
      final char prev = word.charAt(r - 1);
      if (curr >= prev) {
        if (curr > prev)
          ++count;
        if (count == 5)
          ans = Math.max(ans, r - l + 1);
      } else {
        count = 1;
        l = r;
      }
    }

    return ans;
  }
}
// code provided by PROGIEZ

1839. Longest Substring Of All Vowels in Order LeetCode Solution in Python

class Solution:
  def longestBeautifulSubstring(self, word: str) -> int:
    ans = 0
    count = 1

    l = 0
    for r in range(1, len(word)):
      curr = word[r]
      prev = word[r - 1]
      if curr >= prev:
        if curr > prev:
          count += 1
        if count == 5:
          ans = max(ans, r - l + 1)
      else:
        count = 1
        l = r

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.