-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
395.py
52 lines (50 loc) · 1.76 KB
/
395.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
__________________________________________________________________________________________________
sample 28 ms submission
from collections import defaultdict,Counter
class Solution:
def longestSubstring(self, s: str, k: int) -> int:
"""
:type s: str
:type k: int
:rtype: int
"""
if not s:
return 0
counter = Counter(s)
for key,val in counter.items():
if val < k:
s = s.replace(key,'-')
if not '-' in s:
return len(s)
substr_set = set(s.split('-'))
res = 0
for substr in substr_set:
res = max(res,self.longestSubstring(substr,k))
return res
__________________________________________________________________________________________________
sample 12948 kb submission
class Solution:
def longestSubstring(self, s: str, k: int) -> int:
"""
for each char, it is whether:
1. not in the strs
2. more than three times
"""
ans = 0
for n in range(1, 27):
l = 0
counter = {}
# todo = 0
for r in range(len(s)):
if s[r] not in counter:
counter[s[r]] = 0
counter[s[r]] += 1
while len(counter) > n:
counter[s[l]] -= 1
if counter[s[l]] == 0:
counter.pop(s[l])
l += 1
if all([v >= k for v in counter.values()]):
ans = max(ans, r - l + 1)
return ans
__________________________________________________________________________________________________