1839. 所有元音按顺序排布的最长子字符串

89 阅读1分钟

(leetcode.cn/problems/lo…)

image.png

image.png

提示

  • 1 <= word.length <= 5 * 105

  • word 只包含字符 'a''e''i''o' 和 'u' 。

/**
 * @param {string} word
 * @return {number}
 */
var longestBeautifulSubstring = function(word) {
//直接比较字符串大小
    // 计算字符串的长度
    let cLen = 1
    // 判断条件:最多只有五个元音
    let volOfClass = 1
    // 返回的中间值
    let res = 0
    for(let i = 1; i < word.length ;i++){
        // 符合美丽的 计数
        if(word[i] >= word[i-1]) cLen++;
        // 判断有没有超过五个元素
        if(word[i] > word[i-1]) volOfClass++;
        // 不符合美丽的重新计数
        if(word[i] < word[i-1]){cLen = 1;volOfClass = 1};
        // 现在的计数和之前的计数进行比较
        if(volOfClass === 5){res = Math.max(cLen,res)};
    }
    return res
};

[1839. 所有元音按顺序排布的最长子字符串 - 力扣(LeetCode)]