class Solution:
def longestBeautifulSubstring(self, word: str) -> int:
vowels = 'aeiou'
max_len, curr_len, count = 0, 0, 0
prev = ''
for i, c in enumerate(word):
if c not in vowels:
curr_len = 0
count = 0
prev = ''
else:
if curr_len == 0 or c < prev:
curr_len = 1
count = 1 if c == 'a' else 0
else:
if c == prev:
curr_len += 1
elif vowels.find(c) == vowels.find(prev) + 1:
curr_len += 1
count += 1
else:
curr_len = 1 if c == 'a' else 0
count = 1 if c == 'a' else 0
prev = c
if count == 5:
max_len = max(max_len, curr_len)
return max_len
class Solution {
public:
int longestBeautifulSubstring(string word) {
string vowels = "aeiou";
int max_len = 0, curr_len = 0, count = 0;
char prev = 0;
for (int i = 0; i < word.length(); ++i) {
char c = word[i];
if (vowels.find(c) == string::npos) {
curr_len = 0;
count = 0;
prev = 0;
} else {
if (curr_len == 0 || c < prev) {
curr_len = 1;
count = (c == 'a') ? 1 : 0;
} else {
if (c == prev) {
curr_len++;
} else if (vowels.find(c) == vowels.find(prev) + 1) {
curr_len++;
count++;
} else {
curr_len = (c == 'a') ? 1 : 0;
count = (c == 'a') ? 1 : 0;
}
}
prev = c;
if (count == 5) {
max_len = max(max_len, curr_len);
}
}
}
return max_len;
}
};
class Solution {
public int longestBeautifulSubstring(String word) {
String vowels = "aeiou";
int maxLen = 0, currLen = 0, count = 0;
char prev = 0;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (vowels.indexOf(c) == -1) {
currLen = 0;
count = 0;
prev = 0;
} else {
if (currLen == 0 || c < prev) {
currLen = 1;
count = (c == 'a') ? 1 : 0;
} else {
if (c == prev) {
currLen++;
} else if (vowels.indexOf(c) == vowels.indexOf(prev) + 1) {
currLen++;
count++;
} else {
currLen = (c == 'a') ? 1 : 0;
count = (c == 'a') ? 1 : 0;
}
}
prev = c;
if (count == 5) {
maxLen = Math.max(maxLen, currLen);
}
}
}
return maxLen;
}
}
var longestBeautifulSubstring = function(word) {
const vowels = "aeiou";
let maxLen = 0, currLen = 0, count = 0;
let prev = "";
for (let i = 0; i < word.length; i++) {
let c = word[i];
if (!vowels.includes(c)) {
currLen = 0;
count = 0;
prev = "";
} else {
if (currLen === 0 || c < prev) {
currLen = 1;
count = (c === 'a') ? 1 : 0;
} else {
if (c === prev) {
currLen++;
} else if (vowels.indexOf(c) === vowels.indexOf(prev) + 1) {
currLen++;
count++;
} else {
currLen = (c === 'a') ? 1 : 0;
count = (c === 'a') ? 1 : 0;
}
}
prev = c;
if (count === 5) {
maxLen = Math.max(maxLen, currLen);
}
}
}
return maxLen;
};
word
consisting only of lowercase English letters, find the length of the longest substring that contains all five English vowels ('a'
, 'e'
, 'i'
, 'o'
, 'u'
) in order and consecutively (they may appear multiple times, but the order must be strictly a
→ e
→ i
→ o
→ u
). The substring must not skip any vowels or have them out of order, but each vowel can appear multiple times in a row. Return the length of the longest such substring. If no such substring exists, return 0.
word
:'a'
.'a'
.word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
:
'a'
: start a new substring, vowel count = 1, length = 1.'e'
: correct order, vowel count = 2, length = 2.'i'
: correct order, vowel count = 3, length = 3.'a'
: order broken (goes back to 'a'), reset, start new substring.a → e → i → o → u
with no breaks."aaaaeiiiiouuu"
(indices 6 to 18), we see all vowels in order, with length 13.13
.