freq
from character to integer.free
to store characters by their frequency, with the highest frequency at the top.busy
to keep track of 'busy' characters.ans
is less than the length of s
:
a. If the size of busy
is k
, pop from busy
and add the character back to free
.
b. If free
is empty, return an empty string.
c. Pop the top character from free
, append it to ans
, decrement its frequency, and if the frequency is not zero, add it to busy
.ans
.