Longest Substring with At Least K Repeating Characters

Longest Substring with At Least K Repeating Characters

Set Version - 36ms

1
2
3
4
5
6
7
8
9
10
11
class Solution:
def longestSubstring(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
for c in set(s):
if s.count(c) < k:
return max(self.longestSubstring(t, k) for t in s.split(c))
return len(s)

test code

1
2
3
4
5
6
7
8
9
In [87]: s = 'aaabb'
In [88]: s.split('a')
Out[88]: ['', '', '', 'bb']
In [89]: k = 3
In [90]: ss = Solution(); t = ss.longestSubstring(s, k); print(t)
3

leet code