Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sourabh2k15/3cf06904b0c849e7598e7e6efcbb524e to your computer and use it in GitHub Desktop.

Select an option

Save sourabh2k15/3cf06904b0c849e7598e7e6efcbb524e to your computer and use it in GitHub Desktop.
159. Longest Substring with At Most Two Distinct Characters
class Solution {
public:
int lengthOfLongestSubstringTwoDistinct(string s) {
if(s.length() == 0) return 0;
unordered_map<char, int> table;
int begin = 0, end = 0, len = 0, counter = 0;
while(end < s.length()){
char current = s[end];
table[current]++;
if(table[current] == 1) counter++;
end++;
while(counter > 2){
char startchar = s[begin];
if(table.count(startchar) == 1){
table[startchar]--;
if(table[startchar] == 0) counter--;
}
begin++;
}
len = max(len, end - begin);
}
return len;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment