Last updated 3 years ago
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 */