> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Longest Repeating Character Replacement

> Tested Python solution for LeetCode 424 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 424, Medium. Topics: Hash Table, String, Sliding Window. [View on LeetCode](https://leetcode.com/problems/longest-repeating-character-replacement/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 424   # by problem number
lcpy gen -s longest_repeating_character_replacement   # by problem name
```

## Problem

You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.

Return the length of the longest substring containing the same letter you can get after performing the above operations.

### Examples

```
Input: s = "ABAB", k = 2
Output: 4
Explanation: Replace the two 'A's with two 'B's or vice versa.
```

```
Input: s = "AABABBA", k = 1
Output: 4
Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA".
The substring "BBBB" has the longest repeating letters, which is 4.
There may exists other ways to achieve this answer too.
```

### Constraints

1 \<= s.length \<= 10^5
s consists of only uppercase English letters.
0 \<= k \<= s.length

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_repeating_character_replacement/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_repeating_character_replacement/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n) - single pass through string
    # Space: O(1) - at most 26 characters in count dict
    def character_replacement(self, s: str, k: int) -> int:
        """
        Find the length of the longest substring with same character
        after at most k replacements using sliding window approach.
        """
        if not s:
            return 0

        count: dict[str, int] = {}
        left = 0
        max_freq = 0
        max_length = 0

        for right in range(len(s)):
            # Expand window: add character at right pointer
            count[s[right]] = count.get(s[right], 0) + 1
            max_freq = max(max_freq, count[s[right]])

            # Shrink window if needed: if we need more than k replacements
            # Current window size = right - left + 1
            # Characters to replace = window_size - max_freq
            # If characters_to_replace > k, we need to shrink
            if (right - left + 1) - max_freq > k:
                count[s[left]] -= 1
                left += 1

            # Update max length
            max_length = max(max_length, right - left + 1)

        return max_length
```

## Complexity

| Time                              | Space                                      |
| --------------------------------- | ------------------------------------------ |
| O(n) - single pass through string | O(1) - at most 26 characters in count dict |

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
