Awesome python solution


  • 2
    F

    use dict instead of counter:

        def characterReplacement(self, s, k):
            count = {}
            max_count = start = result = 0
            for end in range(len(s)):
                count[s[end]] = count.get(s[end], 0) + 1
                max_count = max(max_count, count[s[end]])
                if end - start + 1 - max_count > k:
                    count[s[start]] -= 1
                    start += 1
                result = max(result, end - start + 1)
            return result
    

    use counter:

        def characterReplacement(self, s, k):
            count = collections.Counter()
            start = result = 0
            for end in range(len(s)):
                count[s[end]] += 1
                max_count = count.most_common(1)[0][1]
                if end - start + 1 - max_count > k:
                    count[s[start]] -= 1
                    start += 1
                result = max(result, end - start + 1)
            return result
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.