Posted on

Description

Submission

class Solution {
public:
    int findTheLongestSubstring(string s) {
        vector<int> pos(1<<5, -2);
        unordered_map<char, int> index{{'a', 0}, {'e', 1}, {'i', 2}, {'o', 3}, {'u', 4}};
        pos[0] = -1;

        int state = 0, ret = 0;
        for(int i = 0; i < s.length(); ++i) {
            if(index.count(s[i])) {
                state = state ^ (1 << index[s[i]]);
            }
            if(pos[state] == -2) pos[state] = i;
            ret = max(ret, i - pos[state]);
        }

        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *