Sort Characters by Frequency

In this tutorial, I am going to discuss a very interesting problem sort characters by frequency.

Given a string, write a method to sort it in decreasing order based on the frequency of characters.

For example:

Example 1

Input : “teee”

Output: “eeet”

Example 2:

  Input: “dddbbb”

Output: “dddbbb”   or “bbbddd” (Both d and b appear 3 times so any of the above output are valid)

  Example 3:

  Input:  “Eett”

Output: “ttEe” or “tteE”  (Both e and E are two different characters so the output should not be “Eett”)

There are multiple ways to solve this problem. In this tutorial, I am going to discuss three approaches to solve this problem. At the end of this tutorial, I have shared the video link of this tutorial.

Programming video tutorials

Find the longest common prefix string

Sort Characters by Frequency using HashMap – Java Code

The easiest approach is to use HashMap to solve this problem. First, we traverse a string and put each character and it’s count in a HashMap. After that sort the HashMap by values.

The time complexity of this approach is O(nlogn) and it’s space complexity is O(n).

Sort Characters by Frequency using HashMap and Priority Queue – Java Code

In our previous example, we sorted the HashMap by values. Instead of sorting the HashMap, we can use priority queue to solve this problem.

While creating an instance of priority queue, passed the custom comparator which arrange the characters based on their count (when characters are added in a priority queue).

Then, Iterate the priority queue the character which has highest frequency comes first then next and so on.

The time complexity of this approach is O(nlogn) and it’s space complexity is O(n).

Sort Characters by Frequency using HashMap and Bucket Sort – Java Code

Let’s improve the time complexity of our previous code. In this example, we are going to solve this problem using HashMap and bucket sort.

Here are the following steps to solve this problem –

Step 1: First step is to traverse a string and put each character and it’s count in a HashMap.

Suppose the string is “teeetfff”. So, each character and it’s counts are store like this (t : 2, e : 3, f : 3).

Find duplicate characters in a string

Step 2: After that create a multi-storage bucket. The size of this bucket will be maximum frequency of character plus one. And, In each storage level store characters with the same frequency.

For example:

bucket[3] = ef, bucket[2] = t

Step 3: Traverse this bucket from descending order. Merge elements with the same frequency and return the final string.

The output is “fffeeett”

We have discussed the algorithm to solve this problem. Let’s implement this algorithm using java code.

The time complexity of this approach is O(n).

Find top k most frequent elements

How to sort a string in decreasing order based on the count of characters: Video Tutorial

Tagged . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.