PROBLEM 12 OF 75

Longest Repeating Character Replacement

Find the longest substring that can become one repeated character using at most k replacements.

PATTERNSliding windowExpand to learn; shrink to restore the rule.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Find the longest substring that can become one repeated character using at most k replacements.

WHY THIS FITS

Window cost is length minus its most frequent character count.

COMPLEXITY

O(n) time · O(k) space

COMMON FAILURE

Recomputing the maximum count every shrink is unnecessary.

THE REASONING, IN ONE PLACE

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.

Open blank recall ↗