Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Sort Characters By Frequency

XPChallenge Points: 20
levelLevel: Medium

You are given a string s.

Count how many times each character appears in the string, then rearrange the characters so that characters with higher frequency appear before characters with lower frequency.

  • If multiple characters have the same frequency, their relative order does not matter.

  • Uppercase and lowercase letters are considered different.

Return the resultant string.

Example 1:

Input: s = "banana"

Output: "aaannb"

Explanation:

'a' appears 3 times, 'n' appears 2 times, 'b' appears onc

Example 2:

Input: s = "xyz"

Output: "xyz"

Explanation:

All characters appear once, so any ordering is valid.

Example 3:

Input: s = "bbAa"

Output: "bbAa"

Explanation:

'b' appears 2 times, 'A' and 'a' appear once each. Case-sensitive characters are treated separately.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result