Back to problems Solve on LeetCode →

Longest Repeating Character Replacement

LeetCode 424 • Medium • Sliding Window

Input: s = "AABABBA", k = 1  →  Output: 4
Replace at most k characters so the substring has all the same letter. Find the longest such substring.

TimeO(n)single pass
SpaceO(1)26-char map
Window: 0 Max Freq: 0 Best: 0
Window
Right (expand)
Shrink
Result
Window
Counts
Result
pending
Ready
Press Play to watch the sliding window expand and contract, or Step to advance one operation.
The window grows right, and shrinks from the left when replacements exceed k.