classSolution:deflengthOfLongestSubstringTwoDistinct(self,s:str) ->int: fast =0 slow =0 memo =defaultdict(int) res =0while fast <len(s): char = s[fast] fast +=1 memo[char]+=1whilelen(memo)>2: d = s[slow] slow +=1 memo[d]-=1if memo[d]==0:del memo[d] res =max(res, fast - slow)return res