其他分享
首页 > 其他分享> > [LeetCode] 1371. 每个元音包含偶数次的最长子字符串

[LeetCode] 1371. 每个元音包含偶数次的最长子字符串

作者:互联网

官方给出答案

class Solution {
    public int findTheLongestSubstring(String s) {
        int n = s.length();
        int[] pos = new int[1 << 5];
        Arrays.fill(pos, -1);
        int ans = 0, status = 0;
        pos[0] = 0;
        for (int i = 0; i < n; i++) {
            char ch = s.charAt(i);
            if (ch == 'a') {
                status ^= (1 << 0);
            } else if (ch == 'e') {
                status ^= (1 << 1);
            } else if (ch == 'i') {
                status ^= (1 << 2);
            } else if (ch == 'o') {
                status ^= (1 << 3);
            } else if (ch == 'u') {
                status ^= (1 << 4);
            }
            if (pos[status] >= 0) {
                ans = Math.max(ans, i + 1 - pos[status]);
            } else {
                pos[status] = i + 1;
            }
        }
        return ans;
    }
}

 

标签:status,偶数,String,int,pos,1371,ans,LeetCode,Math
来源: https://www.cnblogs.com/doyi111/p/12926529.html