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.
Example:
Input String:
csestack
Output String:
aektccss
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.
sorted()
inbuilt function. sorted()
function with the key attribute and Python lambda function.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.
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)
.
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.
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.
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.