# 424. Longest Repeating Character Replacement (ref to  1004

T: O(n)

S: O(1)

```java
class Solution {
    public int characterReplacement(String s, int k) {
        int[] charCount = new int[26]; // all uppercase , use int array to save count
        int left = 0;
        int right = 0;
        int maxCount = 0;
        int result = 0;
        
        while (right < s.length()) {
            charCount[s.charAt(right) - 'A']++;
            maxCount = Math.max(maxCount, charCount[s.charAt(right) - 'A']); // get maximum count
            right++;
            while (right - left - maxCount > k) {
                charCount[s.charAt(left) - 'A']--;
                left++;
            }
            result = Math.max(result, right - left);
        }
        return result;
    }
}

/*
count max char

AABABBA
l
   r -> cal max
  l
      r -> cal
     l  
       r -> shrink left
 
 right - left - max > 1 -> not fit, shrink left
 
 
 right - left - max <= k -> fit result, cal
*/
```
