Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
louzhedong opened this issue Apr 27, 2021 · 0 comments
Open

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

louzhedong opened this issue Apr 27, 2021 · 0 comments

Comments

@louzhedong
Copy link
Owner

习题

当一个字符串满足如下条件时,我们称它是 美丽的 :

所有 5 个英文元音字母('a' ,'e' ,'i' ,'o' ,'u')都必须 至少 出现一次。
这些元音字母的顺序都必须按照 字典序 升序排布(也就是说所有的 'a' 都在 'e' 前面,所有的 'e' 都在 'i' 前面,以此类推)
比方说,字符串 "aeiou" 和 "aaaaaaeiiiioou" 都是 美丽的 ,但是 "uaeio" ,"aeoiu" 和 "aaaeeeooo" 不是美丽的 。

给你一个只包含英文元音字母的字符串 word ,请你返回 word 中 最长美丽子字符串的长度 。如果不存在这样的子字符串,请返回 0 。

子字符串 是字符串中一个连续的字符序列。

示例 1:

输入:word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
输出:13
解释:最长子字符串是 "aaaaeiiiiouuu" ,长度为 13 。
示例 2:

输入:word = "aeeeiiiioooauuuaeiou"
输出:5
解释:最长子字符串是 "aeiou" ,长度为 5 。
示例 3:

输入:word = "a"
输出:0
解释:没有美丽子字符串,所以返回 0 。

提示:

1 <= word.length <= 5 * 105
word 只包含字符 'a','e','i','o' 和 'u' 。

思路

类似这种计算字符串中连续子串,一般都可以采用双指针滑动窗口的方式

另外就是用一个set来保存已经遍历过的元素

解答

javascript

/**
 * @param {string} word
 * @return {number}
 */
var longestBeautifulSubstring = function(word) {
    var stack = [],
        set = new Set(),
        left = 0,
        right = 0,
        res = 0;
    while(right < word.length) {
        if (stack.length == 0 || word[right] >= stack[stack.length - 1]) {
            stack.push(word[right]);
            set.add(word[right]);
            if (set.size == 5) {
                res = Math.max(res, stack.length);
            }
        } else {
            stack = [];
            set.clear();
            stack.push(word[right]);
            set.add(word[right]);
            left = right;
        }
        right++;
    }
    return res;
};

go

func longestBeautifulSubstring(word string) int {
    stack := make([]byte, 0)
    existMap := make([]byte, 0)
    right := 0
    res := 0
    for right < len(word) {
        current := word[right]
        if len(stack) == 0 || current >= stack[len(stack) - 1] {
            stack = append(stack, current)
            isExist := false
            for _, item := range existMap {
                if item == current {
                    isExist = true
                }
            }
            if isExist == false {
                existMap = append(existMap, current)
            }
            if len(existMap) == 5 {
                if len(stack) > res {
                    res = len(stack)
                }
            }
        } else {
            stack = []byte{current}
            existMap = []byte{current}
        }
        right ++
    }
    return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant