424. Longest Repeating Character Replacement


time: O(n)
space: O(1)
class Solution {
public int characterReplacement(String s, int k) {
int maxFreq = 0;
int count[] = new int[26];
int left = 0;
int result = 0;
for (int right = 0; right < s.length(); right++) {
count[s.charAt(right) - 'A']++;
maxFreq = Math.max(maxFreq, count[s.charAt(right) - 'A']);
while (right - left + 1 - maxFreq > k) {
count[s.charAt(left) - 'A']--;
left++;
}
result = Math.max(result, right - left + 1);
}
return result;
}
}
/*
ABAB
i. j
j - i +1 - maxfrq <= k
*/
Previous340. Longest Substring with At Most K Distinct Characters (sliding window, hashmap, two pointers),Next1151. Minimum Swaps to Group All 1's Together
Last updated
Was this helpful?