class Solution(object):
def maxRepOpt1(self, text):
"""
:type text: str
:rtype: int
"""
cnt = collections.Counter(text)
n = len(text)
ans = 0
i = 0
while i < n:
j = i
while j < n and text[j] == text[i]:
j += 1
left = j - i
k = j + 1
while k < n and text[k] == text[i]:
k += 1
right = k - j - 1
ans = max(ans, min(left + right + 1, cnt[text[i]]))
i = j
return ans