Leetcode Link: 928 · 最多有两个不同字符的最长子串 - LintCode
题目

解法一
思路: 用哈希表中的元素数量来判断窗口内有多少个不同的字符
缩窗的时候要及时将数量为0的字符pop出去
题解:
class Solution:
def length_of_longest_substring_two_distinct(self, s: str) -> int:
dd = {}
right = left = 0
res = 0
while(right < len(s)):
if s[right] not in dd.keys():
while(len(dd)>=2):
dd[s[left]] -= 1
if dd[s[left]] == 0:
dd.pop(s[left]) # 从字典中删除这个字符
left += 1
dd[s[right]] = 1
else:
dd[s[right]] += 1
right += 1
res = max(res, right - left)
return res启发和联系
类似问题 无重复字符串的最长子串