Sliding Window
Longest Repeating Character Replacement
medium
DESCRIPTION (inspired by Leetcode.com)
Write a function to find the length of the longest substring containing the same letter in a given string s (consisting of only uppercase English letters), after performing at most k operations in which you can choose any character of the string and change it to any other uppercase English letter. Each operation changes one character at one position, so changing three Bs to As uses 3 operations.
Input:
s = "BBABCCDD"
k = 2
Output:
5
Explanation: Replace the 'A' and the first 'C' with 'B' to form "BBBBBCDD". That uses 2 operations, one for each character changed. The longest substring with identical letters is "BBBBB", which has a length of 5.
Explanation
In order for a substring to be valid, k + the frequency of the most frequent character in the substring must be greater than or equal to the length of the substring.
For example, if k = 2, and the substring is AABBB, then the most frequent character is B, which shows up 3 times. The substring is valid because 2 + 3 >= 5.
However, if k = 2 and the substring is AAABBB, then the substring is invalid because 2 + 3 < 6.
We use this fact to solve this problem with a variable-length sliding window to iterate over all valid substrings, and return the longest of those lengths at the end. To represent the state of the current window, we keep track of two variables:
- state: A dictionary mapping each character to the number of times it appears in the current window.
- max_freq: The maximum number of times a single character has appeared in any window so far.
We start by extending the current window until it becomes invalid (i.e. k + max_freq < window length).
def characterReplacement(s, k):
state = {}
max_freq = 0
max_length = 0
start = 0
for end in range(len(s)):
state[s[end]] = state.get(s[end], 0) + 1
max_freq = max(max_freq, state[s[end]])
if k + max_freq < end - start + 1:
state[s[start]] -= 1
start += 1
max_length = max(max_length, end - start + 1)
return max_length
start
longest repeating character replacement
At this point, the longest substring we have found so far is BCBAB, which has a length of 5 (max_freq = 3 + k = 2).
Now, whenever we try to extend the current window to length 6, it will be invalid unless the character we just included in the window shows up 4 times. So each time we increase the window to length 6, we immediately shrink the window back to length 5 until we find a character which shows up 4 times.
Notice we only shrink by one each time, not in a loop. Since we grow the window by one character per step, it can only become invalid by exactly one position. And because max_freq never decreases (it's a running maximum across all windows), a single shrink is always enough. This is why the code uses if rather than while — it's not a shortcut, it's a property of the algorithm.
max_freq: 3
start: 0 | end: 5
expand window
This continues until we reach the end of the string, at which point we return the length of the longest substring we have found so far.
max_freq: 4
start: 3 | end: 8
expand window
Solution
start
longest repeating character replacement