地址:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
示例代码:
public int longestBeautifulSubstring(String word) {
if (word.length() < 5) {
return 0;
}
// res表示美丽字符串长度,type表示元音字符种类个数,len表示当前截取字符长度
int res = 0, type = 1, len = 1;
for (int i = 0; i < word.length() - 1; i++) {
if (word.charAt(i) <= word.charAt(i + 1)) {
//更新当前字符串长度
len++;
}
if (word.charAt(i) < word.charAt(i + 1)) {
//更新当前字符种类
type++;
}
if (word.charAt(i) > word.charAt(i + 1)) {
//当前字符串不美丽,从当前字符重新开始
type = 1;
len = 1;
}
if (type == 5) {
//更新最大字符串
res = Math.max(res, len);
}
}
return res;
}