Guest User

Untitled

a guest
Jan 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int lengthOfLongestSubstringTwoDistinct(string s) {
  4. if(s.length() == 0) return 0;
  5.  
  6. unordered_map<char, int> table;
  7. int begin = 0, end = 0, len = 0, counter = 0;
  8.  
  9. while(end < s.length()){
  10. char current = s[end];
  11.  
  12. table[current]++;
  13. if(table[current] == 1) counter++;
  14.  
  15. end++;
  16.  
  17. while(counter > 2){
  18. char startchar = s[begin];
  19.  
  20. if(table.count(startchar) == 1){
  21. table[startchar]--;
  22. if(table[startchar] == 0) counter--;
  23. }
  24.  
  25. begin++;
  26. }
  27.  
  28. len = max(len, end - begin);
  29. }
  30.  
  31. return len;
  32. }
  33. };
Add Comment
Please, Sign In to add comment