Given a string s, your task is to rearrange its characters so they appear in decreasing order of frequency. In other words, characters that appear more often should come first in the result.
The frequency of a character is simply how many times it appears in the original string. If two characters have the same frequency, you can arrange them in any order relative to each other.
Example: If the input is "tree", the character 'e' appears 2 times, while 't' and 'r' each appear 1 time. So a valid output would be "eert" or "eetr".
Your goal is to return any valid string that satisfies the frequency sorting requirement.
Input & Output
Visualization
Time & Space Complexity
O(n) to count frequencies + O(n) to fill buckets + O(n) to build result = O(n) total
O(k) for frequency map + O(n) for buckets array where n is string length
Constraints
- 1 โค s.length โค 5 ร 105
- s consists of uppercase and lowercase English letters and digits
- Multiple valid answers exist - return any one of them