-
Notifications
You must be signed in to change notification settings - Fork 0
/
767.py
37 lines (33 loc) · 1.04 KB
/
767.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
# -*- coding: utf-8 -*-
# @Time : 2020/12/1 17:20
# @Author : Yike Cheng
# @FileName: 767.py
# @Software: PyCharm
from heapq import *
import collections
class Solution:
def reorganizeString(self, S: str) -> str:
if len(S) < 2:
return S
length = len(S)
counts = collections.Counter(S)
maxCount = max(counts.items(), key=lambda x: x[1])[1]
if maxCount > (length + 1) // 2:
return ""
queue = [(-x[1], x[0]) for x in counts.items()]
heapify(queue)
print(queue)
ans = []
while len(queue) > 1:
_, letter1 = heappop(queue)
_, letter2 = heappop(queue)
ans.extend([letter1, letter2])
counts[letter1] -= 1
counts[letter2] -= 1
if counts[letter1] > 0:
heappush(queue, (-counts[letter1], letter1))
if counts[letter2] > 0:
heappush(queue, (-counts[letter2], letter2))
if queue:
ans.append(queue[0][1])
return "".join(ans)