---
# System prepended metadata

title: 1371. Find the Longest Substring Containing Vowels in Even Counts
tags: [Leetcode, Bit Manipulation, HashMap, Prefix Sum, Medium]

---

# 1371. Find the Longest Substring Containing Vowels in Even Counts
###### tags: `Leetcode` `Medium` `Prefix Sum` `HashMap` `Bit Manipulation`
Link: https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/
## 思路
和[1915. Number of Wonderful Substrings](https://hackmd.io/0NLt0f9ZQnicvBhDgaKRIg)很像
用mask记录当前几个元音字母出现的次数为奇数还是偶数
如果前面出现过一样的mask就说明现在的subarray所有元音字母都是出现偶数次

## Code
```java=
class Solution {
    public int findTheLongestSubstring(String s) {
        int[] pos = new int[32];
        pos[0]=-1;
        for(int i=1; i<pos.length; i++){
            pos[i]=-2;
        }
        int mask = 0;
        int ans = 0;
        for(int i=0; i<s.length(); i++){
            char c = s.charAt(i);
            if(c == 'a') mask ^= 1<<0;
            else if(c == 'e') mask ^= 1<<1;
            else if(c == 'i') mask ^= 1<<2;
            else if(c == 'o') mask ^= 1<<3;
            else if(c == 'u') mask ^= 1<<4;
            if(pos[mask]!=-2) ans = Math.max(ans, i-pos[mask]);
            else pos[mask] = i;
        }
        return ans;
    }
}
```