双指针,右指针遍历记录字符串出现的个数,当超过条件时,移动左指针直到满足条件,更新最大字符串长度
from collections import defaultdict
import sys
s = sys.stdin.readline().strip()
count = defaultdict(int)
l = r = 0
n = len(s)
res = 0
while r < n:
count[s[r]] += 1
while len(count) > 2 and l <= r - 2:
count[s[l]] -= 1
if count[s[l]] == 0:
del count[s[l]]
l += 1
res = max(res, r - l + 1)
r += 1
print(res)