Longest Repeating Character Replacement

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code
  • Pro

Write characterReplacement(text, k): text is made of capital letters A to Z, and you may change up to k of them to any other capital letter. Return the length of the longest stretch of one repeated letter you can make. k is a whole number, 0 or more, and empty text gives 0. For example, characterReplacement("AABABBA", 1) is 4: change the A in "BABB" to get "BBBB". The checks include 100,000 letters, so trying every stretch is too slow.

Your code must pass

  • Finds the longest stretch after up to k changes
  • Works with no changes allowed
  • Stops at the length of the text when k is large
  • Returns 0 for empty text
  • Is fast enough for 100,000 letters