Practice/Microsoft/Leetcode 451. Sort Characters By Frequency
CodingMust
Given a string containing letters (both uppercase and lowercase) and digits, rearrange the characters so that they appear in descending order based on how frequently they occur in the string. Characters with higher frequency should come first, and all occurrences of the same character must be grouped together.
If multiple characters have the same frequency, they can appear in any order relative to each other.
Example 1:
Input: s = "tree" Output: "eert" Explanation: 'e' appears 2 times, 't' and 'r' each appear 1 time. One valid arrangement is "eert" (another would be "eetr").
Example 2:
Input: s = "cccaaa" Output: "aaaccc" Explanation: Both 'a' and 'c' appear 3 times each. Either "aaaccc" or "cccaaa" is acceptable.
Example 3:
Input: s = "Aabb" Output: "bbAa" Explanation: 'b' appears twice (highest frequency). 'A' and 'a' are different characters, each appearing once.