Blind 75 · Sliding Window

Longest Repeating Character Replacement

Medium

Watch the walkthrough (5:55)

Problem

The input is a string s of capital letters, and a number k.
You may change up to k of its letters, each into any letter you like.
Find the longest substring you can turn into a single repeated letter.
Return its length.

Examples

Example 1:
Input: s = "ABAB", k = 2
Output: 4
Explanation: change both A's (or both B's) and the whole string matches.

Example 2:
Input: s = "AABABBA", k = 1
Output: 4
Explanation: "ABBA" with the lone A flipped gives "BBBB".

Example 3:
Input: s = "AAAA", k = 2
Output: 4

Example 4:
Input: s = "ABCDE", k = 1
Output: 2

Constraints

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

Prerequisites

Sliding windows — a left and right edge moving forward only, and the invariant that decides when the left edge advances.
Hash maps or fixed-size counts for what the window currently holds.

How to think about it

1. Sliding Window, Top Count Read Each Step

Intuition

When the changes pass k, the left edge steps in. A window can be turned into one letter with as many changes as it has letters that are not its most common one: its length less its largest count. Keep a count of each of the 26 letters in the window and read the largest of them every step, so that number is always the window's own. Grow the window on the right; while it needs more than k changes, take its first letter out of the counts and step the left edge in, as many times as it takes — when the most common letter leaves, one step may not be enough.

Algorithm

1. Keep 26 counts, all 0, a left edge at 0, and the best length so far.
2. Move the right edge along the string, adding 1 to the count of its letter.
3. While the window's length less its largest count is more than k, take 1 from the count of the letter at the left edge and step the left edge forward.
4. The best keeps the larger of itself and the window's length.

Time & Space

Time O(n) — each edge moves forward only, and the largest count is read from 26 counts, a fixed number. Space O(1): there are always 26 counts.

2. Window Minus Its Most Common Letter Optimal

Intuition

A window can be made uniform when the letters that are not its most common one number at most k — that is, width minus the highest count is within budget. Grow the right edge, and when the budget is blown, slide the left edge. The subtle part: the highest count is never lowered when the window shrinks, and it does not need to be: a stale value can only make the window look too cheap, never too expensive, but every length the window reaches fits some substring in k changes — so the answer is never over-reported.

Algorithm

1. Count letters inside the window; track the highest count seen.
2. Extend the right edge, updating that count.
3. While width minus the highest count exceeds k, slide the left edge up one.
4. The widest window that was ever valid is the answer.

Time & Space

Time O(n). Space O(1) for a fixed alphabet.