Skip to content

Longest Repeating Character Replacement

MediumSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.

You are given a string s of uppercase letters and a number k. You may change at most k characters to any other uppercase letter. Return the length of the longest substring that can be made of one repeated letter.

s = "ABAB",    k = 2   →  4   (change both A's or both B's)
s = "AABABBA", k = 1   →  4   ("AABA" → "AAAA")
s = "ABCDE",   k = 1   →  2

Constraints

  • 1 <= s.length <= 10^5
  • s consists of uppercase English letters only.
  • 0 <= k <= s.length