9. Longest Happy String
A string is called happy if it does not have any of the strings 'aaa'
, 'bbb'
or 'ccc'
as a substring.
Given three integers a
, b
and c
, return any string s
, which satisfies following conditions:
s
is happy and longest possible.s
contains at mosta
occurrences of the letter'a'
, at mostb
occurrences of the letter'b'
and at mostc
occurrences of the letter'c'
.s
will only contain'a'
,'b'
and'c'
letters.
If there is no such string s
return the empty string ""
.
Example 1:
Example 2:
Example 3:
Solution: (Greedy and Priority Queue)
Approach: (We greedily choose characters)
while (pq.size()>1)
Pop out the first 2 pair who have the max frequency.Append 2 chars(from pair_one) if size is >= 2 (if (pair_one.count >= 2)), otherwise append only one char.
Append 2 chars (from pair_two) if size is >= 2 and pair_one.count < pair_two.count. Otherwise append only one char.
At the end, check if PQ is empty. If not, append if char is not same.
Last updated