Given a string, find the length of the longest substring T that contains at most 2 distinct characters.

from collections import defaultdict
class Solution:
    def lengthOfLongestSubstringTwoDistinct(self, s: str) -> int:
        if not s:
            return 0

        ret = start = 0
        char_map = defaultdict(int)
        for i, c in enumerate(s):
            char_map[c] += 1
            while len(char_map) > 2:
                char_map[s[start]] -= 1
                if char_map[s[start]] == 0:
                    del char_map[s[start]]
                start += 1
            ret = max(ret, i - start + 1)
        return ret