1
class Solution:
2
def reorganizeString(self, s: str) -> str:
3
c = Counter(s) # for counting the distinct element
4
pq = []
5
for k,v in c.items(): heapq.heappush(pq,(-v,k)) #multipy by -1 to make max heap
6
ans = ''
7
while pq :
8
c, ch = heapq.heappop(pq)
9
if ans and ans[-1] == ch:
10
if not pq: return '' // if heap is empty we cant make the ans return empty string
11
c2, ch2 = heapq.heappop(pq)
12
ans += ch2
13
c2 += 1
14
if c2: heapq.heappush(pq,(c2,ch2))
15
else:
16
ans += ch
17
c += 1
18
if c: heapq.heappush(pq,(c,ch))
19
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0