# 424. Longest Repeating Character Replacement

{% embed url="<https://leetcode.com/problems/longest-repeating-character-replacement/>" %}

sliding window，核心问题还是移动左指针的条件。

```python
class Solution:
    def characterReplacement(self, s: str, k: int) -> int:
        res = 0
        freq = defaultdict(int)
        
        l, r = 0, 0
        while r < len(s):
            freq[s[r]] += 1
            
            while r+1-l - max(freq.values()) > k:
                freq[s[l]] -= 1
                l += 1
            
            res = max(res, r+1-l)
            r+=1
            
        return res
```
