Longest Repeating Character Replacement
Problem
You are given a string s of uppercase English letters and an integer k. You may overwrite up to k positions with any uppercase letter you like. Return the length of the longest contiguous block consisting of a single repeated letter that can be produced this way.
shas between 1 and 100,000 characters, all uppercaseA-Z.kis a non-negative integer no larger than the length ofs.
Examples
s = "QQZQZZQ", k = 1→4. Overwrite theZinQQZQ(or theQinZQZZ) to get a uniform block of four; no block of five can be fixed with one edit.s = "XYXY", k = 2→4. Two edits are enough to turn the whole string intoXXXXorYYYY.s = "PQRST", k = 0→1. Nothing can be changed and no two neighbours already match.
Hints
- For a fixed window, which letter should you keep and which ones should you overwrite? How many overwrites does that take?
- A window is fixable when
window length - count of its most common letter <= k. Does this property stay true when you shrink the window? - Sliding window with a 26-entry count table; expand
hi, and when the window needs more thankedits, moveloone step. The window never needs to shrink below the best length already found.
Last reviewed 8 September 2026
Approach · Solution (Python) · Complexity · Follow-ups · Company variations · Tests
The rest of this question — the worked approach, the Python solution with passing tests, complexity, follow-ups and how each company frames it — is part of the guides.
Get the full guide
Pick the company you're interviewing with. Each guide has every question tiered for that loop — one-time purchase, lifetime access.
Also in: Apple, Google, Meta, Microsoft, Netflix
Interviewing at more than one? All 6 guides for $499 · save $395
Not ready to buy? Get the free starter pack — 13 questions with full solutions.