Longest Repeating Character Replacement
Medium · Sliding window
Jump to codeFor a string of uppercase English letters and a budget k, find the longest substring that can become one repeated letter after changing at most k positions.
Examples
Example 1
s = "ABAB" k = 2
Output: 4
Example 2
s = "AABABBA" k = 1
Output: 4
Constraints
- 1 <= s.length <= 1000
- 0 <= k <= s.length
Need a hint?
Required replacements equal window length minus its largest letter count.
Asked in real interviews
Candidates reported versions of Longest Repeating Character Replacement from these company interviews. Some company questions are locked and open with Premium or XP.
- Longest Single-Letter Substring After at Most k Letter ReplacementsMicrosoft
- Count Longest Substrings After Character ReplacementsPinduoduo
- Clarify a Longest-Repeating-Character Replacement ProblemDigitalocean
Solution approach
Expand a window and count letters. While window length minus the current maximum of the 26 counts exceeds k, remove the leftmost character and advance left. Track the largest valid window. Recomputing the maximum keeps the invariant explicit and costs only a fixed factor.
Complexity
O(26n), hence O(n), time and O(26) space.
Python reference solution
def characterReplacement(s, k):
counts = [0] * 26
left = best = 0
for right, char in enumerate(s):
counts[ord(char) - ord('A')] += 1
while right - left + 1 - max(counts) > k:
counts[ord(s[left]) - ord('A')] -= 1
left += 1
best = max(best, right - left + 1)
return best
JavaScript solution
function characterReplacement(s, k) {
const counts = Array(26).fill(0);
let left = 0, best = 0;
for (let right = 0; right < s.length; right++) {
counts[s.charCodeAt(right) - 65]++;
while (right - left + 1 - Math.max(...counts) > k) {
counts[s.charCodeAt(left++) - 65]--;
}
best = Math.max(best, right - left + 1);
}
return best;
}
Java solution
import java.util.*;
class Solution {
public int characterReplacement(String s, int k) {
int[] counts = new int[26];
int left = 0, best = 0, most = 0;
for (int right = 0; right < s.length(); right++) {
most = Math.max(most, ++counts[s.charAt(right) - 'A']);
// A historical maximum is enough when only the best length is needed.
if (right - left + 1 - most > k) counts[s.charAt(left++) - 'A']--;
best = Math.max(best, right - left + 1);
}
return best;
}
}
C++ solution
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int characterReplacement(string s, int k) {
array<int, 26> counts{};
int left = 0, best = 0;
for (int right = 0; right < (int)s.size(); right++) {
counts[s[right] - 'A']++;
while (right - left + 1 - *max_element(counts.begin(), counts.end()) > k) {
counts[s[left++] - 'A']--;
}
best = max(best, right - left + 1);
}
return best;
}
};
Trace a boundary case, then implement the approach in the editor. Submit checks your solution against all 9 test cases.
Keep your reasoning, pseudocode, and edge cases here.
Notes stay in this browser when storage is available.
Accepted submissions mark this problem complete. Progress stays in this browser.
[ "ABAB", 2 ]
4
[ "AABABBA", 1 ]
4
[ "AAAA", 0 ]
4
[ "ABCD", 1 ]
2
[ "AABBB", 2 ]
5
Drafts are saved per language in this browser when storage is available.