Skip to content
Decoder

Longest Happy String

Medium

Problem

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 "".

Example 1

Input
a = 1, b = 1, c = 7
Output
"ccaccbcc"
Why
length 8; "ccbccacc" is also fine

Example 2

Input
a = 7, b = 1, c = 0
Output
"aabaa"
Why
length 5: only one b to split the a's

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.