Longest Happy String LeetCode solution in Python
1class Solution:2def longestDiverseString(self, a: int, b: int, c: int) -> str:3pq = []4if a > 0:5heapq.heappush(pq, (-a, "a"))6if b > 0:7heapq.heappush(pq, (-b, "b"))8if c > 0:9heapq.heappush(pq, (-c, "c"))1011ans = ""12while pq:13c, ch = heapq.heappop(pq)14if len(ans) > 1 and ans[-1] == ans[-2] == ch:15if not pq:16break17c2, ch2 = heapq.heappop(pq)18ans += ch219c2 += 120if c2:21heapq.heappush(pq, (c2, ch2))22else:23ans += ch24c += 125if c:26heapq.heappush(pq, (c, ch))2728return ans