Skip to content

Latest commit

 

History

History
28 lines (26 loc) · 711 Bytes

1156.md

File metadata and controls

28 lines (26 loc) · 711 Bytes

1156. Swap For Longest Repeated Character Substring

Solution 1 (time O(n), space O(1))

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