Longest Repeating Character Replacement
Find the longest substring that can become one repeated character using at most k replacements.
Why does this pattern fit?
Restate the exact job
Find the longest substring that can become one repeated character using at most k replacements.
Window cost is length minus its most frequent character count.
O(n) time · O(k) space
Recomputing the maximum count every shrink is unnecessary.
How to solve Longest Repeating Character Replacement
The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.
What the question asks
Find the longest substring that can become one repeated character using at most k replacements.
Why Sliding window fits
Window cost is length minus its most frequent character count.
State to maintain
Character counts, a left boundary, and maximum frequency seen.
Transition
Expand right; while cost exceeds k, remove left characters.
Time and space
O(n) time · O(k) space
Counterexample to the tempting mistake
Recomputing the maximum count every shrink is unnecessary.
Prove it again tomorrow
Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.