![]()
class Solution {
public int findTheLongestSubstring(String s) {
int n = s.length();
int[] pos = new int[32]; // 11111最大为31,记录所有状态可能的情况开32就可以了
// pos[status] 记录的是前多少个字符可以使状态变为status!
Arrays.fill(pos,-1);
pos[0] = 0; // 前0个字符可以使状态变为0
int res = 0, status = 0; // 5位整数表示出现aeiou的状态,0代表这一位为偶数个,1为奇数个
for(int i = 0; i < n; i++) {
char c = s.charAt(i);
if(c == 'a') {
status ^= 1 << 0;
} else if (c == 'e') {
status ^= 1 << 1;
} else if (c == 'i') {
status ^= 1 << 2;
} else if (c == 'o') {
status ^= 1 << 3;
} else if (c == 'u') {
status ^= 1 << 4;
} // 奇数-偶数=奇数 偶数-奇数= 奇数
if(pos[status] != -1) { // 所以 只有奇数-奇数 偶数-偶数 才会出现偶数,所以只有之前出现过这个状态,之间的个数才是偶数次
res = Math.max(res,i+1-pos[status]);
} else {
pos[status] = i + 1;
}
}
return res;
}
}