1371. 每个元音包含偶数次的最长子字符串
解题思路:
官方的解题思路太好了。。。官网思路 记录一下代码,方便自己日后复查。
源代码:
class Solution {
public:
int findTheLongestSubstring(string s) {
// 用来保存当前序列的状态
int c_s = 0, maxLen = 0;
vector<int> status(100, INT_MIN);
int i = 0, len = s.length();
status[0] = 0;
while(i<len){
if(s[i] == 'a'){
c_s ^= 1;
}else if(s[i] == 'e'){
c_s ^= 1<<1;
}else if(s[i] == 'i'){
c_s ^= 1<<2;
}else if(s[i] == 'o'){
c_s ^= 1<<3;
}else if(s[i] == 'u'){
c_s ^= 1<<4;
}
if(status[c_s] != INT_MIN){
maxLen = max(maxLen, i - status[c_s] + 1);
}else{
status[c_s] = i + 1;
}
i ++;
}
return maxLen;
}
};