-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path767 Reorganize String.py
60 lines (47 loc) · 1.35 KB
/
767 Reorganize String.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
53
54
55
56
57
58
59
60
#!/usr/bin/python3
"""
Given a string S, check if the letters can be rearranged so that two characters
that are adjacent to each other are not the same.
If possible, output any possible result. If not possible, return the empty
string.
Example 1:
Input: S = "aab"
Output: "aba"
Example 2:
Input: S = "aaab"
Output: ""
Note:
S will consist of lowercase letters and have length in range [1, 500].
"""
from collections import defaultdict
class Solution:
def reorganizeString(self, S: str) -> str:
"""
piles by max char and circular append
"""
counter = defaultdict(int)
for c in S:
counter[c] += 1
lst = [
(-n, n, c)
for c, n in counter.items()
]
lst.sort()
piles = []
_, n, c = lst[0]
for i in range(n):
piles.append([c])
cnt = 0
for _, n, c in lst[1:]:
for _ in range(n):
piles[cnt].append(c)
cnt = (cnt + 1) % len(piles)
if len(piles) > 1 and len(piles[-2]) == 1:
return ""
return "".join(
map(lambda x: "".join(x), piles)
)
if __name__ == "__main__":
assert Solution().reorganizeString("vvvlo") == "vlvov"
assert Solution().reorganizeString("aab") == "aba"
assert Solution().reorganizeString("aaab") == ""