15% off all plans & roadmaps Use code
See plansProblem editorial
Implement the Solution class and its longestDiverseString method. The method receives three nonnegative counts: a for the available letter 'a', b for 'b', and c for 'c'.
Longest Happy String is a Heap (Priority Queue) Patterns problem, in the Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern. The pattern covers top K elements, K-way merge, two heaps for median, and scheduling
The reference solution runs in O((a + b + c) log 3), which is O(a + b + c). time and O(1), since the priority queue contains at most three letters. space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Longest Happy String is a Heap (Priority Queue) Patterns problem, in the Scheduling / Minimum Cost (Greedy with Priority Queue) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.