Ctrl K

Three Letters, No Triples

Medium
You have count_a letters a, count_b letters b and count_c letters c. You lay them out into a string one letter at a time, and the string must never contain three equal letters in a row. Build the string greedily, step by step: 1. Among the letters still left, take the one with the most copies remaining. On a tie, take the one that comes first alphabetically. 2. If it would make three equal letters in a row, take instead the best of the other remaining letters by the same rule. If no other letter is left, the string is finished. 3. Append the chosen letter and repeat until the letters run out. Return the resulting string. Think about why this greedy strategy produces a string of the greatest possible length.
Example 1
count_a=1, count_b=1, count_c=5 → 'ccaccbc'

Place "cc"; a third c is not allowed, so take a (a and b are tied and a comes first). Then "cc" again, then b and the last c.

Example 2
count_a=0, count_b=6, count_c=1 → 'bbcbb'

After "bbcbb" only b is left and a third b in a row is not allowed, so the string ends with two b unused.

Example 3
count_a=2, count_b=2, count_c=2 → 'abcabc'
Constraints
  • 0 ≤ count_a, count_b, count_c ≤ 100
  • count_a + count_b + count_c > 0
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNo Twin Letters Side by Side MediumLargest String With a Run Cap MediumFill the Blanks With the Fewest Repeats MediumStars Eat the Smallest Letters MediumEqual Letters Kept Apart Hard
loading Python…
Input
count_a=1, count_b=1, count_c=5
Expected
'ccaccbc'

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