Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int lengthOfLongestSubstringTwoDistinct(string s) {
- unordered_map<char, int> cnt;
- int n=s.length(), left=0, right=0, ans=0;
- for(right=0; right<n; right++){
- cnt[s[right]]++;
- while(left<right && cnt.size()>2){
- if(--cnt[s[left]] == 0) cnt.erase(s[left]);
- left++;
- }
- ans = max(ans, right-left+1);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment