class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s) {
std::queue<char> Q;
std::unordered_map<char,int> count;
int ans=0;
for(char ch:s){
if(count[ch]==0){
int temp=0;
for(unordered_map<char,int>::iterator iter=count.begin();iter!=count.end();iter++){
if(iter->second>0){
temp+=1;
}
}
if(temp<2){
count[ch]+=1;
Q.push(ch);
ans=max(ans,(int)Q.size());
}
else{
while(true){
char ch_temp=Q.front();
Q.pop();
count[ch_temp]-=1;
if(count[ch_temp]==0){
Q.push(ch);
count[ch]+=1;
ans=max(ans,(int)Q.size());
break;
}
}
}
}
else{
count[ch]+=1;
Q.push(ch);
ans=max(ans,(int)Q.size());
}
}
return ans;
}
};
07-01
1148
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-03
566
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)