Grokking Data Structures & Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Sort Characters By Frequency (easy)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given a string, arrange its characters in descending order based on the frequency of each character. If two characters have the same frequency, their relative order in the output string can be arbitrary.

Example

  1. Input: s = "trersesess"
    • Expected Output: "sssseeerrt"
    • Justification: The character s appears four times, e three times, r two times and t only once. All characters are sorted based on their frequnecy.
  2. Input: s = "banana"
    • Expected Output: "aaannb".
    • Justification: The character 'a' appears three times, 'n' twice and 'b' once.
  3. Input: s = "abb"
    • Expected Output: "bba"
    • Justification: The character b appears twice and a` only once.

Constraints:

  • 1 <= s.length <= 5 * 10<sup>5</sup>
  • s consists of uppercase and lowercase English letters and digits.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible