> ## 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 Substring with At Least K Repeating

> Tested Python solution for LeetCode 395 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 395, [Medium](/catalog/medium). Topics: [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Divide and Conquer](/catalog/topics/divide-and-conquer), [Sliding Window](/catalog/topics/sliding-window). [View on LeetCode](https://leetcode.com/problems/longest-substring-with-at-least-k-repeating-characters/description/).

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

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

## Problem

Given a string `s` and an integer `k`, return *the length of the longest substring of* `s` *such that the frequency of each character in this substring is greater than or equal to* `k`.

### Examples

```
Input: s = "aaabb", k = 3
Output: 3
Explanation: The longest substring is "aaa", as 'a' is repeated 3 times.
```

```
Input: s = "ababbc", k = 2
Output: 5
Explanation: The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
```

### Constraints

* `1 <= s.length <= 10^4`
* `s` consists of only lowercase English letters.
* `1 <= k <= 10^5`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(26 * n) -> O(n)
    # Space: O(26) -> O(1)
    def longest_substring_with_at_least_k_repeating_characters(self, s: str, k: int) -> int:
        best = 0
        for target in range(1, 27):
            if target * k > len(s):
                break
            counts: dict[str, int] = {}
            left = 0
            for right, ch in enumerate(s):
                counts[ch] = counts.get(ch, 0) + 1
                while len(counts) > target:
                    counts[s[left]] -= 1
                    if counts[s[left]] == 0:
                        del counts[s[left]]
                    left += 1
                if len(counts) == target and all(c >= k for c in counts.values()):
                    best = max(best, right - left + 1)
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(26 \* n) -> O(n) | O(26) -> O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.