Fundamentals/Sort Characters by Frequency
← PrevNext →
Given a string s, return a new string whose characters are sorted by descending frequency. Characters with the same frequency are ordered by ascending character code (so uppercase letters come before lowercase letters of the same letter).
Input
s: a string of any characters (may be empty)
Output
A string containing the same characters as s, grouped by character and ordered as described.
Examples
Example 1
Inputs = "tree"
Output"eert"
Explanation: 'e' appears twice; 'r' and 't' each appear once and tie-break alphabetically.
Example 2
Inputs = "Aabb"
Output"bbAa"
Explanation: 'b' appears twice; 'A' (code 65) precedes 'a' (code 97) on the tie.
Constraints
0 <= s.length <= 10^5
s may contain any characters.