Leetcode 1839. Longest Substring Of All Vowels in Order Solution
Problem Description
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, you need to return the length of the longest beautiful substring of the word. If no such substring exists, return 0. A substring is a contiguous sequence of characters in a string.
Example 1:
1Input: word = "aeiaaioaaaaeiiiiouuuooaauuaeiu" 2Output: 13 3Explanation: The longest beautiful substring in word is "aaaaeiiiiouuu" of length 13.
Example 2:
1Input: word = "aeeeiiiioooauuuaeiou" 2Output: 5 3Explanation: The longest beautiful substring in word is "aeiou" of length 5.
Example 3:
1Input: word = "a" 2Output: 0 3Explanation: There is no beautiful substring, so return 0.
Constraints:
- 1 <= word.length <= 5 * 105
- word consists of characters 'a', 'e', 'i', 'o', and 'u'.
Approach
A sliding window can be used to solve this problem. Traverse the given string word
, use a variable count
to track the number of distinct vowels encountered in the current contiguous sequence, and maintain two pointers l
(left) and r
(right). Update the value of count
based on the relationship between the adjacent characters. Once count
reaches 5, update the ans
(the answer) if it is less than the current window size. Reset the value of count
and the left pointer l
when a new contiguous sequence starts.
Here's a step-by-step example using the input word="aeiaaioaaaaeiiiiouuuooaauuaeiu"
:
- Initialize
ans=0
,count=1
,l=0
, andr=1
. - Traverse the string
word
from index 1 to the end:- At each position, compare the current character with the previous one.
- If the current character is greater than or equal to the previous one:
- If the current character is greater than the previous one, increment
count
. - If
count
is 5, updateans
with the maximum value betweenans
and the current window size (r-l+1
).
- If the current character is greater than the previous one, increment
- Otherwise, reset
count
to 1 andl
to the current positionr
.
- Return the value of
ans
.
Python Solution:
1class Solution:
2 def longestBeautifulSubstring(self, word: str) -> int:
3 ans = 0
4 count = 1
5
6 l, r = 0, 1
7 while r < len(word):
8 curr = word[r]
9 prev = word[r - 1]
10
11 if curr >= prev:
12 if curr > prev:
13 count += 1
14 if count == 5:
15 ans = max(ans, r - l + 1)
16 else:
17 count = 1
18 l = r
19
20 r += 1
21
22 return ans
Java Solution:
1class Solution {
2 public int longestBeautifulSubstring(String word) {
3 int ans = 0;
4 int count = 1;
5
6 for (int l = 0, r = 1; r < word.length(); ++r) {
7 char curr = word.charAt(r);
8 char prev = word.charAt(r - 1);
9
10 if (curr >= prev) {
11 if (curr > prev) {
12 ++count;
13 }
14 if (count == 5) {
15 ans = Math.max(ans, r - l + 1);
16 }
17 } else {
18 count = 1;
19 l = r;
20 }
21 }
22
23 return ans;
24 }
25}
JavaScript Solution:
1class Solution {
2 longestBeautifulSubstring(word) {
3 let ans = 0;
4 let count = 1;
5
6 for (let l = 0, r = 1; r < word.length; ++r) {
7 const curr = word[r];
8 const prev = word[r - 1];
9
10 if (curr >= prev) {
11 if (curr > prev) {
12 ++count;
13 }
14 if (count === 5) {
15 ans = Math.max(ans, r - l + 1);
16 }
17 } else {
18 count = 1;
19 l = r;
20 }
21 }
22
23 return ans;
24 }
25}
C++ Solution:
1class Solution {
2public:
3 int longestBeautifulSubstring(string word) {
4 int ans = 0;
5 int count = 1;
6
7 for (int l = 0, r = 1; r < word.length(); ++r) {
8 const char curr = word[r];
9 const char prev = word[r - 1];
10
11 if (curr >= prev) {
12 if (curr > prev) {
13 ++count;
14 }
15 if (count == 5) {
16 ans = max(ans, r - l + 1);
17 }
18 } else {
19 count = 1;
20 l = r;
21 }
22 }
23
24 return ans;
25 }
26};
C# Solution:
1public class Solution {
2 public int LongestBeautifulSubstring(string word) {
3 int ans = 0;
4 int count = 1;
5
6 for (int l = 0, r = 1; r < word.Length; ++r) {
7 char curr = word[r];
8 char prev = word[r - 1];
9
10 if (curr >= prev) {
11 if (curr > prev) {
12 ++count;
13 }
14 if (count == 5) {
15 ans = Math.Max(ans, r - l + 1);
16 }
17 } else {
18 count = 1;
19 l = r;
20 }
21 }
22
23 return ans;
24 }
25}
In all the provided solutions, the longestBeautifulSubstring
function follows the same sliding window approach and updates the answer variable (ans
) based on the value of count
.The time complexity of these solutions is O(N), where N is the length of the input string 'word'. This is because the sliding window approach iterates through the entire string once. The space complexity is O(1) since only a constant amount of space is used for the variables. These solutions successfully determine the length of the longest beautiful substring of the input string by following the given conditions and requirements.