Longest substring where you can replace at most K characters to make all chars the same. Β β± Time
O(n)Β πΎ SpaceO(1)
Use a sliding window. A window is valid if window_size - max_freq β€ k β meaning we can replace all non-dominant characters within budget k. When the window becomes invalid, slide the left pointer right. The max_freq never needs to decrease because we're looking for the maximum window.
Key insight:
replacements_needed = window_length - count_of_most_frequent_char
if replacements_needed > k β shrink window from left
s = "AABAABABA", k = 2
A A B A A B A B A
L R window="A", max_freq=1, len-max=0 β€ 2 β
L R window="AAB", max_freq=2, 3-2=1 β€ 2 β
L R window="AABA", max_freq=3, 4-3=1 β€ 2 β
L R window="AABAA", max_freq=4, 5-4=1 β€ 2 β
L R window="AABAAB", max_freq=4, 6-4=2 β€ 2 β
L R window="AABAABA", 7-4=3 > 2 β shrink!
L R window="ABAABA", ...
Result: 6
This is LeetCode 424, a classic sliding window problem. The elegant insight β that max_freq never needs to decrease β makes it a favourite interview question to test whether candidates truly understand the sliding window pattern.
- The key trick:
max_freqcan only increase as we look for a longer valid window β never reset it - Window constraint:
(right - left + 1) - max(freq.values()) > k - Space is effectively O(1) because there are only 26 uppercase letters
- Don't overcomplicate with frequency recalculations β stale
max_freqis fine - Variation: apply same pattern to binary strings (flip 0s to 1s)
- DNA / genomic sequence analysis
- String compression preprocessing
- Typewriter/autocorrect optimization
- Finding optimal character normalisation windows
- Longest Substring β sliding window without replacement
- Subsets β different exploration of string windows