Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

139. Sort Characters By Frequency

MediumDay 41 of 60Heap / Priority Queue

Given a string s, reorder its characters so that they appear in decreasing order of how often they occur. All copies of a character must sit together. Characters with the same frequency may appear in any order. Uppercase and lowercase letters are different characters.

Example 1
Input: s = "tree"
Output: "eert"
'e' appears twice, 'r' and 't' once each. "eetr" is also accepted.
Example 2
Input: s = "Aabb"
Output: "bbAa"
"bbaA" is also accepted, but "bAab" is not because the b's are split.
Constraints
  • 1 <= s.length <= 5 * 10^5
  • s has uppercase and lowercase English letters and digits
Hints
Frequently asked at: Amazon, Bloomberg, Google
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min