------
## 最终结论
```python
def characterReplacement(s: str, k: int) -> int:
    left = 0
    max_length = 0
    count = [0] * 26  # To count occurrences of each character

    for right in range(len(s)):
        count[ord(s[right]) - ord('A')] += 1
        
        # Check if we need to shrink the window
        while (right - left + 1) - max(count) > k:
            count[ord(s[left]) - ord('A')] -= 1
            left += 1
        
        max_length = max(max_length, right - left + 1)

    return max_length
```