-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0767.Reorganize_String.py
More file actions
50 lines (36 loc) · 1.22 KB
/
0767.Reorganize_String.py
File metadata and controls
50 lines (36 loc) · 1.22 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
40
41
42
43
44
45
46
47
48
49
50
"""
Given a string s, rearrange the characters of s so that any two adjacent characters are not the same.
Return any possible rearrangement of s or return "" if not possible.
Example 1:
Input: s = "aab"
Output: "aba"
Example 2:
Input: s = "aaab"
Output: ""
Constraints:
1 <= s.length <= 500
s consists of lowercase English letters.
"""
class Solution:
def reorganizeString(self, s: str) -> str:
hq = []
backadd = deque()
freqs = Counter(list(s))
for val, freq in freqs.items():
heappush(hq, (-freq, val))
res = ""
while len(hq) > 0:
for _ in range(2):
if len(hq) == 0 and len(backadd) > 0: #要注意这里的判断,容易忘记
return ""
if len(hq) > 0:
freq, val = heappop(hq)
freq = -freq
freq -= 1
if freq > 0:
backadd.append((-freq, val))
res += val
if len(hq) == 0 and len(backadd) == 0:
return res
while len(backadd) > 0:
heappush(hq, backadd.pop())