InferenceAI

Enter at least two characters to search.

Start free

Sort Letters by Frequency

mediumArrays & Strings25 min25 XP

Return s with its characters sorted by decreasing frequency. Break ties by character code, smallest first, so the answer is unique.

Examples

EXAMPLE WALKTHROUGHe twice

A guided view of the published example, not a trace of your code.

INPUT["tree"]
TASKsort_by_frequency()
EXPECTED OUTPUT"eert"

Start with the input values shown in this example.

Example 1 of 3

Input: s = "tree"

Output: "eert"

Explanation: e twice

Input: s = "cccaaa"

Output: "aaaccc"

Explanation: tie

Input: s = "Aabb"

Output: "bbAa"

Explanation: case-sensitive

Hint 1

Count with a Counter, then sort the distinct characters by (-count, char).

Topics

DSAArrays & StringsPython