A string is considered beautiful if it satisfies the following conditions:
'a', 'e', 'i', 'o', 'u') must appear at least once in it.'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 * 105word consists of characters 'a', 'e', 'i', 'o', and 'u'.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for finding the longest vowel substring means checking every possible substring within the given text. We'll consider each possible starting point and ending point for a substring. For each substring, we then verify if it only contains vowels in the required order and if it is longer than the best substring we've found so far.
Here's how the algorithm would work step-by-step:
def longest_substring_of_all_vowels_in_order_brute_force(input_string):
longest_substring_length = 0
for i in range(len(input_string)):
for j in range(i, len(input_string)):
substring = input_string[i:j+1]
is_all_vowels = True
for char in substring:
if char not in 'aeiou':
is_all_vowels = False
break
if is_all_vowels:
# Check if the vowels are in order
is_in_order = True
expected_vowel_index = 0
vowels = 'aeiou'
for char in substring:
if char == vowels[expected_vowel_index]:
if expected_vowel_index < 4:
expected_vowel_index += 1
elif char in vowels[expected_vowel_index:]:
is_in_order = False
break
else:
is_in_order = False
break
# Update the longest substring length if the substring meets the conditions.
if is_in_order:
substring_length = len(substring)
longest_substring_length = max(longest_substring_length, substring_length)
return longest_substring_lengthTo find the longest valid substring, we'll use a focused approach that efficiently scans the input. We carefully track our progress through the vowels and reset when we encounter an invalid sequence, avoiding unnecessary checks. This method only examines each character a few times, which makes it super fast.
Here's how the algorithm would work step-by-step:
def longest_substring_of_all_vowels_in_order(word):
vowels = 'aeiou'
vowel_index = 0
max_length = 0
current_length = 0
for char in word:
if char == vowels[vowel_index]:
current_length += 1
vowel_index += 1
# If we've reached the last vowel, reset the index.
if vowel_index == len(vowels):
max_length = max(max_length, current_length)
vowel_index = 0
else:
# Start building a new string if current char is 'a'.
if char == 'a':
vowel_index = 1
current_length = 1
# Otherwise, reset and search from this point onwards.
else:
max_length = max(max_length, current_length)
current_length = 0
vowel_index = 0
max_length = max(max_length, current_length)
return max_length| Case | How to Handle |
|---|---|
| Empty input string | Return 0 as there are no vowels in order. |
| Input string with no vowels | Return 0 as there is no valid substring. |
| Input string with only one vowel | Return 1 if the single character is a vowel, 0 otherwise. |
| Input string with vowels in reverse order or mixed order | The algorithm should correctly identify substrings that violate the vowel order and not include them in the longest substring calculation. |
| Input string containing only 'a's | Return the length of the string. |
| Input string like 'aeiou' | Return the length of the string since it's a valid substring. |
| Long input string to ensure efficiency (e.g., length = 10^5) | The algorithm should have a time complexity that allows it to process long strings without timing out, ideally O(n). |
| Input with repeated valid substrings interspersed with invalid characters, e.g. 'aaeei oou' | The algorithm should reset the valid substring count when an invalid vowel or non-vowel is encountered. |