跳转至

每个元音包含偶数次的最长子字符串⚓︎

Leetcode题目链接

描述⚓︎

详见中文题目链接

解答⚓︎

class Solution {
private:
    int modify(char& ch) {
        switch (ch) {
            case 'a' : return 0;
            case 'e' : return 1;
            case 'i' : return 2;
            case 'o' : return 3;
            case 'u' : return 4;
            default: return -1;
        }
    }

public:
    int findTheLongestSubstring(string s) {
        int n = s.length();
        array<int, 32> prefixMap;
        fill(prefixMap.begin(), prefixMap.end(), -2);
        prefixMap[0] = -1;

        int res = 0, status = 0;
        for (int i = 0; i < n; i++) {
            int bit = modify(s[i]);
            if (bit != -1) {
                status ^= 1 << bit;
            }

            if (prefixMap[status] != -2) {
                res = max(res, i - prefixMap[status]);
            } else {
                prefixMap[status] = i;
            }
        }

        return res;
    }
};