LeetCode 1839: Longest Substring Of All Vowels in Order Solution
Master LeetCode problem 1839 (Longest Substring Of All Vowels in Order), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1839. Longest Substring Of All Vowels in Order
Problem Explanation
Explanation
To solve this problem, we can iterate through the input string and maintain a sliding window that contains all the vowels in alphabetical order. We can keep track of the current vowel being considered and the count of each vowel encountered so far. Whenever we encounter a vowel that is not in the correct order, we reset the window to start from the current vowel. We update the maximum beautiful substring length whenever we find a valid beautiful substring.
Algorithm:
- Initialize variables:
maxLen(maximum beautiful substring length),currLen(current beautiful substring length),currVowelIndex(index of the current vowel in the alphabetical order). - Iterate through the input string:
a. If the current character is the next vowel in alphabetical order, increment
currLen, updatecurrVowelIndex, and update the count of the current vowel. b. If the current character is the same as the current vowel, incrementcurrLen. c. If the current character is a vowel but not in the correct order, resetcurrLento 1 and updatecurrVowelIndex. d. If the current substring is beautiful, updatemaxLenifcurrLenis greater. - Return
maxLen.
Time Complexity: O(N), where N is the length of the input string. Space Complexity: O(1)
Solution Code
class Solution {
public int longestBeautifulSubstring(String word) {
int maxLen = 0, currLen = 1, currVowelIndex = 0;
int[] count = new int[5];
count[word.charAt(0) - 'a']++;
for (int i = 1; i < word.length(); i++) {
char currChar = word.charAt(i);
char prevChar = word.charAt(i - 1);
if (currChar == prevChar) {
currLen++;
} else if (currChar == "aeiou".charAt(currVowelIndex + 1)) {
currLen++;
currVowelIndex++;
} else if (currChar == 'a') {
currLen = 1;
currVowelIndex = 0;
count = new int[5];
count[0]++;
} else {
currLen = 1;
currVowelIndex = 0;
count = new int[5];
count[currChar - 'a']++;
}
if (currVowelIndex == 4) {
maxLen = Math.max(maxLen, currLen);
}
count[currChar - 'a']++;
}
return maxLen;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1839 (Longest Substring Of All Vowels in Order)?
This page provides optimized solutions for LeetCode problem 1839 (Longest Substring Of All Vowels in Order) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1839 (Longest Substring Of All Vowels in Order)?
The time complexity for LeetCode 1839 (Longest Substring Of All Vowels in Order) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1839 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1839 in Java, C++, or Python.