DEV Community

Metete Welete
Metete Welete

Posted on • Edited on

Sort Characters By Frequency - Leetcode

Sort Characters By Frequency

While this is a problem with difficulty at medium, I actually enjoyed solving it and had it run on first write of code.

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.

I first had to break it down by making a string that holds all the value as unique values and also created a map of the values and the number of times they appeared in the given string.

I then looped through the reverse of length of the strings and joined each value as they appeared in the map in a new string.

This isn't the most optimal solution as it takes O(n^2) to run.

I'll update this article when I get a more optimal solution.

Top comments (0)