-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem395.py
More file actions
43 lines (36 loc) · 785 Bytes
/
problem395.py
File metadata and controls
43 lines (36 loc) · 785 Bytes
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
class Solution(object):
def longestSubstring(self, s, k):
"""
:type s: str
:type k: int
:rtype: int
"""
if len(s) < k:
return ''
character_pos = {}
character_cnt = {}
for i in range(len(s)):
if s[i] not in character_cnt:
character_pos[s[i]] = [i]
character_cnt[s[i]] = 1
else:
character_pos[s[i]].append(i)
character_cnt[s[i]] += 1
min_val = min(character_cnt.values())
if min_val >= k:
return s
else:
key = 0
for i in character_pos:
if character_cnt[i] == min_val:
key = i
break
if len(character_pos[0]) == 1:
for i in range(len(character_pos[i]) - 1):
print character_cnt
print character_pos
if __name__ == '__main__':
Solution = Solution()
s = 'aaabb'
k = 3
Solution.longestSubstring(s,k)