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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/two-pointer/sliding-window/424.-longest-repeating-character-replacement-ref-to-1004.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
