# LeetCode 395. Longest Substring with At Least K Repeating Characters

## Description

Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times.

Example 1:

```Input:
s = "aaabb", k = 3

Output:
3

The longest substring is "aaa", as 'a' is repeated 3 times.
```

Example 2:

```Input:
s = "ababbc", k = 2

Output:
5

The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times.
```

## 描述

```输入:
s = "aaabb", k = 3

3

```

```输入:
s = "ababbc", k = 2

5

```

### 思路

• 统计每个字符出现的次数，用次数少于 k 的字符对原字符串进行拆分。递归地对字符串进行同样的操作。
• 在字符串中出现次数少于 k 的字符，一定不能在最长字符串里面，所以用此字符串对原字符串进行切割。如果所有的字符串出现的此处都大于 k，则返回字符串的长度。
```# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-08-27 21:07:09

from collections import Counter

class Solution:
def longestSubstring(self, s: str, k: int) -> int:

for key, v in Counter(s).items():
if v < k:
return max(self.longestSubstring(sub, k) for sub in s.split(key))
return len(s)
```