A string s is called happy if it satisfies the following conditions:
s only contains the letters 'a', 'b', and 'c'.
s does not contain any of "aaa", "bbb", or "ccc" as a substring.
s contains at mosta occurrences of the letter 'a'.
s contains at mostb occurrences of the letter 'b'.
s contains at mostc occurrences of the letter 'c'.
Given three integers a, b, and c, return the longest possible happy string. If there are multiple longest happy strings, return any of them. If there is no such string, return the empty string "".
A substring is a contiguous sequence of characters within a string.
Example 1:
Input: a = 1, b = 1, c = 7
Output: "ccaccbcc"
Explanation: "ccbccacc" would also be a correct answer.
Example 2:
Input: a = 7, b = 1, c = 0
Output: "aabaa"
Explanation: It is the only correct answer in this case.
Constraints:
0 <= a, b, c <= 100
a + b + c > 0
Solutions
Solution 1: Greedy + Priority Queue
Thinking
\(a+b+c\le 300\) would allow search, but the optimum follows a local rule: always spend the letter that remains most often, without three identical characters in a row.
If the last two characters are already that letter, take the second-most instead. A max-heap by remaining count implements this: pop, append if legal, otherwise pop the next, then push leftovers back.
Stop when no letter can be appended, which yields the longest happy string.
The greedy strategy is to prioritize the selection of characters with the most remaining occurrences. By using a priority queue or sorting, we ensure that the character selected each time is the one with the most remaining occurrences (to avoid having three consecutive identical characters, in some cases, we need to select the character with the second most remaining occurrences).