Skip to content

Longest Repeating Character Replacement

MediumPython 3Sliding windowStringsHash tables

You are given a string s of uppercase English letters and an integer k. You may change at most k characters of s into any other uppercase letter. Return the length of the longest substring that can be made to contain a single repeated letter after those changes.

Constraints:
  - 1 <= len(s) <= 10^5
  - s consists of uppercase English letters only
  - 0 <= k <= len(s)
Examples:
  "ABAB", k = 2    -> 4   (change both A's, or both B's)
  "AABABBA", k = 1 -> 4   ("AABA" -> "AAAA", or "ABBA" -> "BBBB")
  "AAAA", k = 0    -> 4   (nothing to change)
  "ABCDE", k = 0   -> 1

Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.

Loading the editor…