Longest Repeating Character Replacement
Longest Repeating Character Replacement is LeetCode 424 (Medium). You get a string s of uppercase letters and a number k. You may change up to k characters, each into any other uppercase letter.
- Return the length of the longest substring that can be turned into one letter repeated.
The string can hold 100,000 characters, so checking every substring is too slow; the answer has to come from one pass.
- 1 <= s.length <= 10⁵
- s consists of only uppercase English letters.
- 0 <= k <= s.length
Intuition
Take any window of the string. The cheapest way to make it one letter is to keep its most common letter and change all the others. So a window needs (its length − the count of its most common letter) changes, and it is usable when that number is at most k.
- Grow: move the right end one letter at a time, counting letters as they enter.
- Slide: when the window needs more than
kchanges, move the left end one step. The window keeps its length instead of shrinking, because only a longer window could beat the best one found so far.
A request for the longest contiguous stretch that stays within a budget, where adding letters can only raise the cost, is a two-pointer window. Max Consecutive Ones III is the same idea with only two symbols.
Approach
Before reading on: for one fixed window, how many changes does it need? Then decide what to move when that number passes k. Aim for O(n) time.
Count the letters in the window
Keep count, how often each letter appears between left and right, and top, the highest count any letter has reached. There are only 26 letters, so count stays tiny.
Add the next letter
For each right, add one to count[s[right]] and raise top if that letter is now the most common. The number of changes the window needs is right - left + 1 - top.
Move the left end when the budget is passed
If the window needs more than k changes, take one from count[s[left]] and step left forward. One letter was just added to a window that fitted, so one step brings the length back to that earlier window's length.
Why `top` never has to go down
After the left end moves, top can be higher than any count still in the window. That is safe: the window only grows past the best length when some letter reaches a count above the old top, and then top is exact again. A too-high top only blocks windows that could not win.
Keep the longest window
After the check, set best to the larger of best and right - left + 1, and return best at the end. The window is never longer than a window that really fitted the budget.
Code in Python, C++ and Java
Common pitfalls
Measuring the window before moving the left end
best = max(best, right - left + 1)
while right - left + 1 - top > k:
count[s[left]] -= 1
left += 1while right - left + 1 - top > k:
count[s[left]] -= 1
left += 1
best = max(best, right - left + 1)On "AABABBA" with k = 1 the window "AABAB" needs 2 changes, but measuring it first records 5. The answer is 4.
Indexing uppercase letters from 'a'
count = [0] * 26
count[ord(ch) - ord('a')] += 1count = [0] * 26
count[ord(ch) - ord('A')] += 1The string is uppercase, so ord('A') - ord('a') is -32 and even "A" raises an IndexError. In C++ the same slip reads outside the array without any error.
Edge cases
No window ever needs more than k changes, so the left end never moves and the answer is the whole length.
No changes are allowed, so the answer is the longest run of one letter: 3 here.
Complexity
s.Longest Repeating Character Replacement FAQ
How is this different from Longest Substring with At Most K Distinct Characters?
There k caps how many different letters a window may hold and nothing is changed. Here any letters may appear, and k caps how many of them you change, so the test is length minus the top count.
Why not try each of the 26 letters as the target?
That also works: for each letter, run a window that allows at most k other letters, and keep the best. It costs 26 passes. Using the window's most common letter does all 26 in one pass, because that letter is always the cheapest one to keep.