Sort Characters by Frequency
Medium
LC #451
hash mapcountingsortingNot attempted yet
Given a string s, return a string with the same
characters, arranged so that characters that occur more
often come first. All copies of a character must be
next to each other.
Characters with the same count may appear in any order. Upper- and lower-case letters are different characters.
Example 1
Input: s = "tree"
Output: "eetr"
"e" appears twice. "eert" is also accepted.
Example 2
Input: s = "abbccc"
Output: "cccbba"
Example 3
Input: s = "Aabb"
Output: "bbAa"
"bbaA" is also accepted.
Constraints
- 0 ≤ s.length ≤ 2·10⁵
- s holds ASCII English letters and digits.