Python program to equal character frequencies Last Updated : 16 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given a String, ensure it has equal character frequencies, if not, equate by adding required characters. Input : test_str = 'geeksforgeeks' Output : geeksforgeeksggkkssfffooorrr Explanation : Maximum characters are 4 of 'e'. Other character are appended of frequency 4 - (count of chars). Input : test_str = 'geksforgeeks' Output : geeksforgeeksggksffoorr Explanation : Maximum characters are 3 of 'e'. Other character are appended of frequency 3 - (count of chars). Method #1 : Using count() + max() + loop In this, we get the frequency of the maximum occurring character, and then append each character to match the maximum character frequency. Python3 # Python3 code to demonstrate working of # Equal character frequencies # Using max() + count() + loop # initializing string test_str = 'geeksforgeeks' # printing original string print("The original string is : " + str(test_str)) # getting maximum frequency character max_freq = max([test_str.count(ele) for ele in test_str]) # equating frequencies res = test_str for chr in test_str: # if frequencies don't match max_freq if res.count(chr) != max_freq: res += chr * (max_freq - test_str.count(chr)) # printing result print("Equal character frequency String : " + str(res)) OutputThe original string is : geeksforgeeks Equal character frequency String : geeksforgeeksggkkssfffooorrr Time Complexity: O(n)Auxiliary Space: O(n) Method #2 : Using Counter() + loop Similar to the above, the difference being Counter() is used to get the maximum element count. Python3 # Python3 code to demonstrate working of # Equal character frequencies # Using Counter() + loop from collections import Counter # initializing string test_str = 'geeksforgeeks' # printing original string print("The original string is : " + str(test_str)) # getting maximum frequency character # using Counter() freq_dict = Counter(test_str) max_freq = test_str.count(max(freq_dict, key = freq_dict.get)) # equating frequencies res = test_str for chr in test_str: # if frequencies don't match max_freq if res.count(chr) != max_freq: res += chr * (max_freq - test_str.count(chr)) # printing result print("Equal character frequency String : " + str(res)) OutputThe original string is : geeksforgeeks Equal character frequency String : geeksforgeeksggkkssfffooorrr Time Complexity: O(n)Auxiliary Space: O(n) Comment More infoAdvertise with us Next Article Odd Frequency Characters - Python M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python - Expand Character Frequency String Given a string, which characters followed by its frequency, create the appropriate string. Examples: Input : test_str = 'g7f2g3i2s2b3e4' Output : gggggggffgggiissbbbeeee Explanation : g is succeeded by 7 and repeated 7 times. Input : test_str = 'g1f1g1' Output : gfg Explanation : f is succeeded by 1 4 min read Python - Expand Character Frequency String Given a string, which characters followed by its frequency, create the appropriate string. Examples: Input : test_str = 'g7f2g3i2s2b3e4' Output : gggggggffgggiissbbbeeee Explanation : g is succeeded by 7 and repeated 7 times. Input : test_str = 'g1f1g1' Output : gfg Explanation : f is succeeded by 1 4 min read Odd Frequency Characters - Python The task of finding characters with odd frequencies in a string in Python involves identifying which characters appear an odd number of times. For example, in the string "geekforgeeks," characters like 'r', 'o', 'f', and 's' appear an odd number of times. The output will be a list of these character 3 min read Odd Frequency Characters - Python The task of finding characters with odd frequencies in a string in Python involves identifying which characters appear an odd number of times. For example, in the string "geekforgeeks," characters like 'r', 'o', 'f', and 's' appear an odd number of times. The output will be a list of these character 3 min read Odd Frequency Characters - Python The task of finding characters with odd frequencies in a string in Python involves identifying which characters appear an odd number of times. For example, in the string "geekforgeeks," characters like 'r', 'o', 'f', and 's' appear an odd number of times. The output will be a list of these character 3 min read Maximum Frequency Character in String - Python The task of finding the maximum frequency character in a string involves identifying the character that appears the most number of times. For example, in the string "hello world", the character 'l' appears the most frequently (3 times).Using collection.CounterCounter class from the collections modul 3 min read Like