> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/two-pointer/sliding-window/424.-longest-repeating-character-replacement.md).

# 424. Longest Repeating Character Replacement

![](/files/-MkzemVUQn3F-hD-x-EH)

![](/files/-Mkzep3BVWMglIjQXvsq)

time: O(n)

space: O(1)

```java
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
*/
```
