Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gauravkr0071/f280e5661155dcb6d091a5aeabac30e4 to your computer and use it in GitHub Desktop.
Save gauravkr0071/f280e5661155dcb6d091a5aeabac30e4 to your computer and use it in GitHub Desktop.
Longest Substring with At Most Two Distinct Characters
int lengthOfLongestSubstringTwoDistinct(string s) {
vector<int> map(128, 0);
int counter=0, begin=0, end=0, d=0;
while(end<s.size()){
if(map[s[end++]]++==0) counter++;
while(counter>2) if(map[s[begin++]]--==1) counter--;
d=max(d, end-begin);
}
return d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment