Skip to content

Practice Sort Characters By Frequency

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#451
LeetCode Problem

Sort Characters By Frequency

Problem Statement

Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string. Return the sorted string. If there are multiple answers, return any of them.

Source: Sort Characters By Frequency on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
s = "tree"
Output
"eert"

# Example 2

Input
s = "cccaaa"
Output
"aaaccc"

# Example 3

Input
s = "Aabb"
Output
"bbAa"

Constraints

1 <= s.length <= 5 * 10^5
s consists of uppercase and lowercase English letters and digits.

Related LeetCode Problems

Practice more problems similar to Sort Characters By Frequency.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.