Skip to contentKEEP THE CRAFTTHE SANCTUARY

Longest Repeating Character Replacement

Find the longest substring that can become one repeated letter after at most k replacements.

The problem

Implement Solution.characterReplacement(s, k). s holds between 1 and 100,000 uppercase English letters, and 0 <= k <= len(s). You may replace at most k characters of s, each with any uppercase letter. Return the length of the longest substring that can be made of one repeated letter this way. For s = "AABABBA" and k = 1, replacing the B in "AABA" gives "AAAA", so the answer is 4.

Starter code

class Solution:
    def characterReplacement(self, s, k):
        pass

Public examples

  1. s = "ABAB", k = 2gives4
  2. s = "ZZZZ", k = 0gives4
  3. s = "ABCD", k = 4gives4

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.