[Solved] Sort Characters by Frequency in Python String

[Solved] Sort Characters by Frequency in Python String

This is one of the questions asked in the coding interview. This question looks difficult. But, Python makes it easy, if you have an understanding of basic Python programming.

Problem Statement:

The characters in the string should be sorted based on the following conditions.

  1. Sort the characters in the string by their frequency of occurrences (the number of times characters have occurred in the string).
  2. If the two different characters have the same frequency, these letters should be sorted based on their alphabetical order.

Example:

Input String:

csestack

Output String:

aektccss

How to Sort Characters by Frequency in Python?

Method 1: Using sorting

There are two methods for sorting in Python. Here, we are using sorted() inbuilt Python function for sorting the string.

Here are the steps to follow.

  • To fulfill the second condition, let’s sort the string using sorted() inbuilt function.
  • After that, sort the array based on the frequency of the characters. For which we are using sorted() function with the key attribute and Python lambda function.

Python Program:

msg_given = 'csestack'
msg_alpha = sorted(msg_given)
sorted_list = sorted(msg_alpha, key=lambda c: msg_alpha.count(c))
final_msg = "".join(sorted_list)
print(final_msg)

Output:

aektccss

Note: sorted() function returns the list of the characters. We have to convert it back to the string after sorting. For which, we can use join() operation.

Writing the same code in a more programmatic way.

def sort_by_freq(msg):
    msg_sort = sorted(msg)
    msg_sort_frq = sorted(msg_sort, key= lambda c: msg_sort.count(c))
    return "".join(msg_sort_frq)

out = sort_by_freq("csestack")
print(f"String sorted by char frequency: {out}")

Output:

String sorted by char frequency: aektccss

Sometimes, in a coding interview, the interviewer may ask you to write your own code for sorting algorithm, instead of using the inbuilt function.

Time Complexity:

Believe, sorted() function takes O(n log n) time for string the string, where n is the length of the string.

For counting each character in the string using count() function, it takes O(n^2) time.

The overall time complexity to sort characters by frequency in Python is O(n^2).

Method 2: Using Dictionary (Optimized)

In this solution, we are going to use Python dictionary.

This is the most optimistic solutuion. If you are aksed this coding challenge in the job interview, use this method. They expect most optimized code.

Python program:

def sort_by_freq(msg):
    frq = [0] * 26
    freq = {} #{char: freq, }
    for ch in msg:
        if freq.get(ch):
            freq[ch] += 1
        else:
            freq[ch] = 1
    
    out = ""
    for k, v in freq.items():
        out += (k*v) 
    
    return out
        

out = sort_by_freq("csestack")
print(f"String sorted by char frequency: {out}")

Output:

String sorted by char frequency: ccssetak

In this solution, the alphabetical order of the characters is also maintained.

Complexity:

As we are traversing the string one character at a time, the time complexity of the program is O(n).

To solve this problem we are using dictionary. In worst case (if all the characters in the strings are unique), the size of the dictionary will be same as lenght of the string. So the space complexity is 0(n).

If you can solve this problem in any other ways, share your solution in the comment.

Leave a Reply

Your email address will not be published. Required fields are marked *