Ctrl K

Two-Color Garland

Medium
A machine builds a garland from red red bulbs ('a') and blue blue bulbs ('b'). To keep three bulbs of one color from ending up in a row, it hangs them in portions: - while both colors remain: if more red than blue bulbs are left, it hangs "aab"; if more blue are left, "bba"; if the counts are equal, "ab"; - once one color runs out, all the remaining bulbs of the other color go at the end. Return the finished garland as a string of 'a' and 'b'. The input is such that three equal bulbs never end up in a row.
Example 1
red=5, blue=3 → 'aabaabab'

5 > 3, so 'aab' goes up, leaving 3 and 2. 3 > 2, another 'aab', leaving 1 and 1. Equal, so 'ab'. Result: 'aabaabab'.

Example 2
red=1, blue=4 → 'bbabb'

More blue, so 'bba', leaving 0 and 2. Red is used up, so the two blue bulbs go at the end.

Example 3
red=2, blue=0 → 'aa'
Constraints
  • 0 ≤ red, blue ≤ 100
  • red ≤ 2·(blue + 1) and blue ≤ 2·(red + 1)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Number From Pieces MediumEvery Letter Once MediumErase Digits for the Smallest Number MediumSmallest Up-Down Sequence MediumRing of Flippable Words Medium
loading Python…
Input
red=5, blue=3
Expected
'aabaabab'

Run checks the first 3 tests in your browser. Submit checks all 38 tests on the server, hidden ones included.