Solution
from collections import Counter
class Solution:
def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
cnt = Counter()
ans = j = 0
for i, c in enumerate(s):
cnt[c] += 1
while len(cnt) > 2:
cnt[s[j]] -= 1
if cnt[s[j]] == 0:
cnt.pop(s[j])
j += 1
ans = max(ans, i - j + 1)
return ans
if __name__ == "__main__":
sol = Solution()
print(sol.lengthOfLongestSubstringTwoDistinct("eceba"))
print(sol.lengthOfLongestSubstringTwoDistinct("ccaabbb"))