Python – Sort Strings by maximum frequency character
Given a string, the task is to write a Python program to perform sort by maximum occurring character.
Input : test_list = [“geekforgeeks”, “bettered”, “for”, “geeks”]
Output : [‘for’, ‘geeks’, ‘bettered’, ‘geekforgeeks’]
Explanation : 1 < 2 < 3 < 4, is ordering of maximum character occurrence frequency.
Input : test_list = [“geekforgeeks”, “for”, “geeks”]
Output : [‘for’, ‘geeks’, ‘geekforgeeks’]
Explanation : 1 < 2 < 4, is ordering of maximum character occurrence frequency.
Method #1 : Using sort() + Counter() + max()
In this, we perform inplace sorting using sort(), and Counter() is used to compute characters frequency, max() used to get maximum of computed frequencies.
Python3
from collections import Counter
def max_freq(arg_str):
res = Counter(arg_str)
return arg_str.count( max (res, key = res.get))
test_list = [ "geekforgeeks" , "bettered" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
test_list.sort(key = max_freq)
print ( "Sorted List : " + str (test_list))
|
Output:
The original list is : ['geekforgeeks', 'bettered', 'for', 'geeks']
Sorted List : ['for', 'geeks', 'bettered', 'geekforgeeks']
Method #2 : Using sorted() + lambda + Counter()
In this, we perform the task of performing sort using sorted(), and lambda function to get a single statement logical expression, avoiding external function call.
Python3
from collections import Counter
def max_freq(arg_str):
res = Counter(arg_str)
return arg_str.count( max (arg_str, key = arg_str.get))
test_list = [ "geekforgeeks" , "bettered" , "for" , "geeks" ]
print ( "The original list is : " + str (test_list))
res = sorted (test_list,
key = lambda arg_str : arg_str.count(
max (Counter(arg_str), key = Counter(arg_str).get)))
print ( "Sorted List : " + str (res))
|
Output:
The original list is : ['geekforgeeks', 'bettered', 'for', 'geeks']
Sorted List : ['for', 'geeks', 'bettered', 'geekforgeeks']
The time and space complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Using dictionary and sorting:
Approach:
In this approach, we first create a dictionary that stores the count of the most occurring character for each string. Then, we sort the list based on the values of this dictionary. If two or more strings have the same count of the most occurring character, we sort them based on their original order in the input list.
Python3
def max_char_freq(string):
freq_dict = {}
for s in string:
max_char = max ( set (s), key = s.count)
freq_dict[s] = s.count(max_char)
return freq_dict
def sort_by_max_freq(string_list):
freq_dict = max_char_freq(string_list)
return sorted (string_list, key = lambda x: (freq_dict[x], string_list.index(x)))
test_list = [ "geekforgeeks" , "bettered" , "for" , "geeks" ]
print (sort_by_max_freq(test_list))
|
Output
['for', 'geeks', 'bettered', 'geekforgeeks']
Time Complexity: O(nlogn) where n is the length of the input list
Space Complexity: O(n)
Last Updated :
10 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...