Skip to content

Longest Happy String

MediumHeap / Priority Queue
Decode it first. Clues, constraints, pattern and plan before any code.

A string is happy if it only uses the letters a, b and c, and never contains "aaa", "bbb" or "ccc" (no letter three times in a row).

Given three numbers a, b and c, build the longest possible happy string that uses at most a copies of 'a', b copies of 'b' and c copies of 'c'. If several longest strings exist, return any of them. If none can be built, return "".

a = 1, b = 1, c = 7  →  "ccaccbcc"   (length 8; "ccbccacc" is also fine)
a = 7, b = 1, c = 0  →  "aabaa"      (length 5: only one b to split the a's)

Constraints

  • 0 <= a, b, c <= 100
  • a + b + c > 0