Trains the technique from
LeetCode 1405Longest Happy StringThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
Three trays hold lettered tiles: a tiles stamped 'a', b tiles stamped 'b' and c tiles stamped 'c'.
Lay tiles out in a row, taking no more from a tray than it holds, so that nowhere in the row does the same letter appear three times running.
Return the longest row that can be laid, written as the string of its letters. Where several rows tie for longest, return the one that comes first in dictionary order.
Example 1
The lone `a` and lone `b` open three places for `c` tiles, counting the two ends, and each place holds at most two, so only six of the seven `c` tiles can be laid and the row runs to eight tiles. Opening with `a` would strand one more `c` tile, so the dictionary-first row of that length starts with a pair of `c` tiles.
Example 2
The single `b` tile splits the row into two places for `a` tiles, each holding at most two, so four of the seven `a` tiles go down and the row runs to five.
Example 3
No tray is dominant enough to leave anything stranded, so all five tiles are laid, and taking the earliest letter possible at each step gives the dictionary-first way of doing it.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def longest_diverse_string(a: int, b: int, c: int) -> str:public String longestDiverseString(int a, int b, int c)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.