242. Sort Characters By Frequency

MediumStringString

Given a string s, sort its characters in decreasing order of frequency. Characters with the same frequency are ordered by their character value (ascending) for a deterministic result. Return the resulting string.

Input: A quoted string s.

Output: Quoted string — frequency-sorted.

Examples

Example 1
Input: "tree"
Output: "eert"
Explanation: e appears twice (first), then r,t.
Example 2
Input: "cccaaa"
Output: "aaaccc"
Explanation: Both appear 3 times; a before c.
Example 3
Input: "Aabb"
Output: "bbAa"
Explanation: b twice; then A,a.

Constraints

Asked by

WiproFlipkartBloombergIBMAmazonAccenture
Solve this problem in the editor →