Question DSA-HEAP-0063
HardWhat is the output of this code?
import heapq
def reorganize(s):
from collections import Counter
h = [(-c, ch) for ch, c in Counter(s).items()]
heapq.heapify(h)
out, prev = [], None
while h:
c, ch = heapq.heappop(h)
out.append(ch)
if prev: heapq.heappush(h, prev)
prev = (c + 1, ch) if c + 1 < 0 else None
return "".join(out) if len(out) == len(s) else ""
print(reorganize("aaabbc"), repr(reorganize("aaab")))
- A.
abacab ''
- B.
ababac ''
- C.
Sign up free to see the answer and a step-by-step explanation.