Sort Characters By Frequency - Problem

Given a string s, your task is to rearrange its characters so they appear in decreasing order of frequency. In other words, characters that appear more often should come first in the result.

The frequency of a character is simply how many times it appears in the original string. If two characters have the same frequency, you can arrange them in any order relative to each other.

Example: If the input is "tree", the character 'e' appears 2 times, while 't' and 'r' each appear 1 time. So a valid output would be "eert" or "eetr".

Your goal is to return any valid string that satisfies the frequency sorting requirement.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "tree"
โ€บ Output: "eetr" or "eert"
๐Ÿ’ก Note: The character 'e' appears 2 times, while 't' and 'r' each appear 1 time. Since 'e' has the highest frequency, it comes first in the result.
example_2.py โ€” Multiple Same Frequencies
$ Input: s = "cccaaa"
โ€บ Output: "cccaaa" or "aaaccc"
๐Ÿ’ก Note: Both 'c' and 'a' appear 3 times each. Since they have the same frequency, either can come first in the result.
example_3.py โ€” Single Character
$ Input: s = "Aab"
โ€บ Output: "bbA" or "bAb"
๐Ÿ’ก Note: The character 'b' appears 1 time, 'A' appears 1 time, and 'a' appears 1 time. Any arrangement is valid since all frequencies are equal.

Visualization

Tap to expand
Sorting Characters by FrequencyInput Analysis: "tree"tfreq: 1rfreq: 1efreq: 2Frequency BucketsFrequency 0[ ]Frequency 1[t, r]Frequency 2[e]Frequency 3+[ ]Result Building (High to Low Frequency)eetr= "eetr"๐ŸŽฏ Algorithm Insight1. Count frequencies: O(n) time2. Bucket sort by frequency: O(n) time3. Build result string: O(n) timeTotal: O(n) optimal complexity!Max frequency โ‰ค n enables bucket sort
Understanding the Visualization
1
Count Song Plays
Go through listening history and count how many times each song was played
2
Create Popularity Buckets
Create buckets for each play count (1 play, 2 plays, 3 plays, etc.)
3
Group Songs by Popularity
Place each song in the bucket corresponding to its play count
4
Build Playlist
Start with highest play count bucket and add all songs, then move to lower counts
Key Takeaway
๐ŸŽฏ Key Insight: By using bucket sort instead of comparison-based sorting, we can reduce the time complexity from O(n + k log k) to O(n), making it optimal for this problem where the maximum frequency is bounded by the string length.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

O(n) to count frequencies + O(n) to fill buckets + O(n) to build result = O(n) total

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

O(k) for frequency map + O(n) for buckets array where n is string length

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค s.length โ‰ค 5 ร— 105
  • s consists of uppercase and lowercase English letters and digits
  • Multiple valid answers exist - return any one of them
Asked in
Amazon 45 Google 38 Microsoft 25 Meta 22 Apple 18
89.8K Views
Medium-High Frequency
~15 min Avg. Time
3.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen