Longest Repeating Character Replacement
Find the longest run of one repeated letter you can make by changing at most k letters.
Given a string sksk
Example 1
Output: 4
Explanation: change both A's to B's, or both B's to A's.
Example 2
Output: 4
Explanation: change the middle A to B to get "AABBBBA", which contains "BBBB".
The Idea
Picture the letters of sk
Take Example 2, AABABBAk = 1AABAk = 1
Look at the picture below, where each row is one stretch, outlined in blue, and the letters to change are orange.
The row labelled AABA has 4 letters and 3 A's, so it needs 1 change and fits. The row labelled AABAB adds the B at index 4: 5 letters, still 3 A's, so 2 changes, which is too many. The row labelled BABB starts at index 2 and has 3 B's, so it fits with 1 change. Changing its A gives BBBB, the answer of Example 2.
A string of 7 letters has 28 stretches, and one of 10,000 letters has about 50 million. Do we have to check them all? Look again at AABAB. Adding a letter to a stretch adds one to its length but at most one to the count of its most common letter, so the changes it needs never go down. Every longer stretch that starts at index 0 therefore needs at least 2 changes too.
So once a stretch needs too many changes, growing it further is pointless, and its start should move right instead. One stretch is then enough: its right end takes in one new letter at a time, and its left end moves forward only when the stretch needs more than k
Solution Approach
Turning that idea into code means settling three things: how to count the changes a window needs, how far the left end moves when the window needs too many, and how to know the count of the most common letter as letters leave.
Counting the changes in a window
How do we know how many changes the window needs? The simple way checks every stretch on its own, with one loop for the start and one for the end, which means about n²/2 stretches for n letters. The window needs less work: it keeps an array of 26 counts, one per letter, and changes one count by one whenever a letter enters or leaves. The changes it needs are its length minus its highest count.
Moving the left end
When the window needs too many changes, how far should its left end move? One way is to keep removing letters on the left until the window fits again. But removing a single letter is enough, because we only care about a window longer than the best one so far.
Each step adds one letter on the right. If the window then needs too many changes, removing one letter on the left brings it back to its old length, the longest found so far. A shorter window could never beat that, so there is no reason to shrink further. The window slides one place right, keeping its size, and grows only when a window one letter longer fits.
A window that has slid may itself need too many changes. In Example 2, the window ends as ABBA, at indexes 3 to 6, which needs 2 changes with k = 1
Keeping the highest count
When letters leave the window, the highest count inside it can drop. Finding it again would mean scanning all 26 counts. Instead, the code keeps maxCount
The window only slides once its length has reached maxCount + kmaxCount + 1maxCount
Java Solution
The method characterReplacement
- First, create
, an array of 26 numbers, one per letter.count turns a letter into its place in the array, 0 for A up to 25 for Z. Sets.charAt(i) - 'A' ,left andmaxCount to 0.best - A loop moves
overright , one letter at a time. The new letter's count goes up by one, ands becomes the larger of itself and that count.maxCount - The window is
, which hass[left..right] letters. If that length minusright - left + 1 is more thanmaxCount , the letter atk leaves: its count drops by one andleft moves one place right.left - Keep the longest window length in
, and return it when the loop is done.best
The mainABABABCAAAAABBBACCCCD
public class LongestRepeatingCharacterReplacement {
public static int characterReplacement(String s, int k) {
int[] count = new int[26];
int left = 0;
int maxCount = 0; // highest count of one letter seen in a window
int best = 0;
for (int right = 0; right < s.length(); right++) {
maxCount = Math.max(maxCount, ++count[s.charAt(right) - 'A']);
// letters to replace = window size - most common letter
if (right - left + 1 - maxCount > k) {
count[s.charAt(left) - 'A']--;
left++; // slide the window instead of growing it
}
best = Math.max(best, right - left + 1);
}
return best;
}
static void show(String s, int k) {
System.out.println(s + ", k = " + k + ": " + characterReplacement(s, k));
}
public static void main(String[] args) {
show("ABAB", 2);
show("AABABBA", 1);
show("ABAB", 0);
show("ABC", 5);
show("AAAA", 0);
show("ABBBACCCCD", 1);
}
}
Running it prints:
ABAB, k = 2: 4
AABABBA, k = 1: 4
ABAB, k = 0: 1
ABC, k = 5: 3
AAAA, k = 0: 4
ABBBACCCCD, k = 1: 5
With k = 0ABABABCk = 5right - left + 1right - leftifABAB, k = 0
Look at the last line, ABBBACCCCDk = 1maxCountmaxCount
The original implementation,
Complexity
The work is one pass of the window's right end over s
- Time: O(n), because
visits each of the n letters once,right only moves forward, and each step changes at most two counts and compares a few numbers.left - Space: O(1), because the
array always has 26 entries, one per uppercase letter, however longcount is.s
For an array of ones and zeros, the same idea is