Longest Repeating Character Replacement | Sliding Window Explained (LeetCode 424)

Try it yourself. The full written explainer and an interactive visualizer are here: https://unrote.com/problems/longest-r... Longest Repeating Character Replacement (LeetCode 424) looks tricky, but it comes down to one clean idea. Given a string and a budget of k rewrites, you want the longest stretch you can turn into a single repeated letter. The whole solution turns on one observation: the number of rewrites a window needs is its length minus the count of its most frequent letter. Keep that at or below k and the window is achievable. From there it is a sliding window. Push the right end out and keep a tally of the letters inside, tracking the highest single-letter count. While the window costs more rewrites than k, pull the left end in. Track the longest valid window you ever see, and that length is the answer. Because the left end only moves forward, both ends together cross the string once, so the whole thing runs in linear time with constant extra space. This walkthrough builds the intuition from zero, runs the window live on a worked example, and explains the elegant detail that makes it O(n). Chapters: 0:00 The challenge 0:27 The problem 0:43 A worked example 0:55 Brute force 1:09 Why brute force is slow 1:21 The key insight 1:40 Keep the majority letter 1:58 The sliding window 2:15 The validity rule 2:34 Run it: grow the window 2:53 Over budget, shrink left 3:14 Slide to the end 3:30 Why it is O(n) 3:52 Time and space 4:08 Recap