-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2182. Construct String With Repeat Limit
More file actions
39 lines (33 loc) · 1.04 KB
/
Copy path2182. Construct String With Repeat Limit
File metadata and controls
39 lines (33 loc) · 1.04 KB
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
class Solution(object):
def repeatLimitedString(self, s, repeatLimit):
"""
:type s: str
:type repeatLimit: int
:rtype: str
"""
cnt = [0]*26
for i in s:
ind = ord(i) - ord('a')
cnt[ind] += 1
ans = ""
for i in range(25,-1,-1):
if cnt[i] == 0:
continue
p,tot = 0,cnt[i]
while tot:
p += 1
tot -= 1
ans += chr(ord('a') + i)
if p == repeatLimit and tot:
p = 0
ok = True
for j in range(i-1,-1,-1):
if cnt[j]:
# print(chr(ord('a') + j))
ans += chr(ord('a') + j)
cnt[j] -= 1
ok = False
break
if ok:
break
return ans