Longest Substring Of All Vowels in Order - Complete Solution Guide
Longest Substring Of All Vowels in Order is LeetCode problem 1839, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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 b
Detailed Explanation
The problem asks us to find the longest substring within a given string `word` that meets two criteria to be considered 'beautiful': 1) It must contain all five vowels ('a', 'e', 'i', 'o', 'u') at least once. 2) The vowels must appear in alphabetical order (e.g., 'a' before 'e', 'e' before 'i', and so on). The input `word` consists only of vowels. If no such beautiful substring exists, the function should return 0.
Solution Approach
The provided solution uses a sliding window technique. It iterates through the string using a right pointer `r` and maintains a left pointer `l` to define the current window. It keeps track of the count of distinct vowels encountered in the current window. If the current character `word[r]` is less than the previous character `word[r-1]`, it means the alphabetical order is broken. In that case, the window is reset by moving the left pointer `l` to the current position `r` and resetting the vowel count to 1 (as the current character represents the start of a potentially new beautiful substring). If `word[r]` is greater than `word[r-1]`, it means a new distinct vowel is encountered so we increase the count. When the count reaches 5, meaning all vowels are present, the current window length (r - l + 1) is compared with the maximum length found so far, and the maximum is updated.
Step-by-Step Algorithm
- Step 1: Initialize `ans` (the maximum length), `l` (left pointer), and `count` (vowel count) to 0, 0, and 1 respectively.
- Step 2: Iterate through the string `word` from index 1 (the second character) to the end using a right pointer `r`.
- Step 3: Inside the loop, compare the current character `word[r]` with the previous character `word[r-1]`.
- Step 4: If `word[r]` is less than `word[r-1]`, reset the left pointer `l` to `r` and the vowel count `count` to 1 (start a new potential substring).
- Step 5: If `word[r]` is greater than `word[r-1]`, increment the vowel count `count` by 1 (a new vowel is encountered).
- Step 6: If `count` equals 5, it means all vowels are present in the current window. Update `ans` with the maximum of its current value and the length of the current window (r - l + 1).
- Step 7: After the loop finishes, return the value of `ans`.
Key Insights
- Insight 1: The problem can be solved efficiently using a sliding window approach. We can expand the window as long as the vowels are in order and reset when we encounter a vowel that breaks the alphabetical order.
- Insight 2: We need to keep track of the number of distinct vowels seen in the current window to determine if all five vowels are present.
- Insight 3: Resetting the window start and vowel count when out-of-order vowels are encountered is crucial for finding the correct longest substring.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: String, Sliding Window.
Companies
Asked at: Thomson Reuters.