Given the string s, return the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times.

&nbsp;
Example 1:

Input: s = "eleetminicoworoep"
Output: 13
Explanation: The longest substring is "leetminicowor" which contains two each of the vowels: e, i and o and zero of the vowels: a and u.


Example 2:

Input: s = "leetcodeisgreat"
Output: 5
Explanation: The longest substring is "leetc" which contains two e's.


Example 3:

Input: s = "bcbcbc"
Output: 6
Explanation: In this case, the given string "bcbcbc" is the longest because all vowels: a, e, i, o and u appear zero times.


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 5 x 10^5
	s&nbsp;contains only lowercase English letters.


class Solution:
    def findTheLongestSubstring(self, s: str) -> int:
        integrals = [(False, False, False, False, False)] # integrals[10][mapping["a"]] == False means we have seen "a" appears even times before index 10
        mapping = {
            "a": 0,
            "i": 1,
            "u": 2,
            "e": 3,
            "o": 4
        }

        for v in s:
            vector = list(integrals[-1])
            if v in mapping: # if v is a vowel
                vector[mapping[v]] = not vector[mapping[v]] # toggle that dimension, because if v had appeared even times before, it becomes odd times now
            integrals.append(tuple(vector))

        seen = {}
        res = 0

        for i, v in enumerate(integrals):
            if v in seen: # we have seen this vector before
                res = max(res, i - seen[v]) # compare its substring length
            else:
                seen[v] = i # just record the first time each vector appears

        return res
class Solution {
     public int findTheLongestSubstring(String s) {
        int res = 0 , mask = 0, n = s.length();
        HashMap<Integer, Integer> seen = new HashMap<>();// key--> Mask, value--> Index
        seen.put(0, -1);
        for (int i = 0; i < n; ++i) {
            if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u'){ // check only vowels and skip consonant
                int c=s.charAt(i);
                mask=mask ^ c;
                seen.putIfAbsent(mask, i);
            }            
            res = Math.max(res, i - seen.get(mask));
        }
        return res;
    }
}
//When the xor of all the even times numbers are done it results in 0. The xor of the vowels are done by indicating
//them with a single digit and the xor value is stored in a map
class Solution {
public:
    int findTheLongestSubstring(string s) {
        int x= 0;
        unordered_map<int,int>mp;
        mp[0]=-1;
        int n=0;
        for(int i=0;i<s.length();i++){
            if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u'){
                x^= (s[i]-'a'+1);
               if(mp.find(x)==mp.end())
                  mp[x]=i;
            }
            if(mp.find(x)!=mp.end())
                n= max(n,i-mp[x]);
        }
        return n;
    }
};
var findTheLongestSubstring = function(s) {
    // u o i e a
    // 0 0 0 0 0 => initial state, all are even letters
    // s = "abcab"
    // 0 0 0 0 1 => at index 0, only a is odd count
    // 0 0 0 0 1 => at index 1, max = 1
    // 0 0 0 0 1 => at index 2, max = 2
    // 0 0 0 0 0 => at index 3, max = 4
    // 0 0 0 0 0 => at index 4, max = 5

    // valid condition: same state in previous index, then it means we have a even count for all letters within the middle substring.
    var mask = 0;
    var t = "aeiou";
    var count = new Map(); // <mask, first_idx>
    count.set(0,-1);
    var res = 0;
    for(var i = 0; i<s.length; i++)
    {
        if(t.indexOf(s[i])>=0)
        {
            var j = t.indexOf(s[i]);
            mask = mask ^ (1 << j);
        }
        if(!count.has(mask))
        {
            count.set(mask,i);
        }
        else
        {
            // substring is from [prevIdx+1, i];
            res = Math.max(res, i - count.get(mask));
        }
    }
    return res;
};
Find the Longest Substring Containing Vowels in Even Counts