message: Update longest-substring-with-at-most-two-distinct-characters.py
diff: @@ -10,13 +10,34 @@ class Solution(object):
  if visited[ord(char)] == 0:
  distinct_count += 1
  visited[ord(char)] += 1
  -
  while distinct_count > 2:
  visited[ord(s[start])] -= 1
  if visited[ord(s[start])] == 0:
  distinct_count -= 1
  start += 1
  -
  longest = max(longest, i - start + 1)
  return longest
  +
  +# Time: O(n)
  +# Space: O(1)
  +from collections import Counter
  +
  +
  +class Solution2(object):
  + def lengthOfLongestSubstringTwoDistinct(self, s):
  + """
  + :type s: str
  + :rtype: int
  + """
  + counter = Counter()
  + left, max_length = 0, 0
  + for right, char in enumerate(s):
  + counter[char] += 1
  + while len(counter) > 2:
  + counter[s[left]] -= 1
  + if counter[s[left]] == 0:
  + del counter[s[left]]
  + left += 1
  + max_length = max(max_length, right-left+1)
  + return max_length
  