Longest Happy String LeetCode solution in Python

1
class Solution:
2
def longestDiverseString(self, a: int, b: int, c: int) -> str:
3
pq = []
4
if a > 0:
5
heapq.heappush(pq, (-a, "a"))
6
if b > 0:
7
heapq.heappush(pq, (-b, "b"))
8
if c > 0:
9
heapq.heappush(pq, (-c, "c"))
10

11
ans = ""
12
while pq:
13
c, ch = heapq.heappop(pq)
14
if len(ans) > 1 and ans[-1] == ans[-2] == ch:
15
if not pq:
16
break
17
c2, ch2 = heapq.heappop(pq)
18
ans += ch2
19
c2 += 1
20
if c2:
21
heapq.heappush(pq, (c2, ch2))
22
else:
23
ans += ch
24
c += 1
25
if c:
26
heapq.heappush(pq, (c, ch))
27

28
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0