
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Equal Character Frequencies in Python
Python consists of numerous built-in functions that are designed to perform a wide range of operations including string manipulation, array analysis, algebraic reasoning etc. Strings in python is a data type in which characters are arranged in a sequence. These characters are indexed to acquire specific position in a sequence, we can initiate basic string operations with the help of these index values. This article will elaborate on a similar string-based operation where we will modify an input string to equalize the character frequency.
Understanding the Problem
Our task is to modify a string in a manner that each character has equal frequencies or number of occurrences. In a string, each character could possess different frequencies and therefore they should be adjusted accordingly. Let's understand this with the help of an example.
Input Output Scenarios
Let us consider a string ?
Input: "thisisateststring"
Here, the maximum occurring character is "t" with a frequency of "4". We have to manipulate the string in a manner that each character from the original string has a frequency of "4". Therefore, the output would be ?
Output: thisisateststringhhhiaaaeeerrrnnnggg
Now that we have understood the problem, let's discuss some effective solutions.
Using Loops (iterations) and Count()
We will run a function and take an input string. We will iterate through this string and count the frequency of each letter with the help of "count()" method.
The count() method returns an integer value. We will obtain the maximum character count value and the maximum occurring letter by establishing a condition which checks the current count. Once, we have obtained these values we will use another iteration to traverse through the original string and isolate those letters whose count is not equal to the maximum count value.
These isolated letters will be appended to a new string such that each letter shares the same frequency. In this manner a new string will be created with equal character frequency.
Example
Following is an example to equalise the character frequencies ?
def INP_str(str): print(f"The original string is {str}") max_count = 0 max_letter = '' for x in str: count = str.count(x) if count > max_count: max_count = count max_letter = x print(f"The maximum occurring letter is {max_letter}") print(f"The maximum frequency is {max_count}") # matching the frequencies newSTR = str for i in str: if newSTR.count(i) != max_count: newSTR += i * (max_count - str.count(i)) print(f"New string with equal character frequency:{newSTR}") INP_str("thisisateststring")
Output
The original string is thisisateststring The maximum occurring letter is t The maximum frequency is 4 New string with equal character frequency:thisisateststringhhhiaaaeeerrrnnnggg
Using Counter() from Collections Module
This approach thrives on the same concept of finding out the maximum character count and the maximum occurring letter. The difference lies in the technique of isolating these parameters. We will use a ?Counter' class instance to count a hash able object (string in this case) and store them in the form of key-value pairs. Here, unique characters are stored as keys and their frequencies are stored as dictionary values.
This approach allows us to execute more advanced operations based on the character counts. Once we have obtained the maximum frequency, we will follow the same process of appending the unequal letters.
Example
Following is an example ?
from collections import Counter def INPstr(input_str): refStr = Counter(input_str) max_count = 0 max_letter = "" for char, count in refStr.items(): if count > max_count: max_count = count max_letter = char print(f"The maximum occurring letter is {max_letter}") print(f"The maximum frequency is {max_count}") newSTR = input_str for i in input_str: if newSTR.count(i) != max_count: newSTR += i * (max_count - input_str.count(i)) print(f"New string with equal character frequency:{newSTR}") INPstr("thisisateststring")
Output
The maximum occurring letter is t The maximum frequency is 4 New string with equal character frequency:thisisateststringhhhiaaaeeerrrnnnggg
Appending Logic
The letters should not exceed the maximum frequency value. We defend this logic by implementing a condition which checks the count of each and every letter and then append only those letters whose frequencies do not match with the maximum frequency. Each character is multiplied with the difference between the maximum frequency value and the current count for that character and then appended to the original string.
Conclusion
Throughout the course of this article, we discussed the different solutions to equalise character frequencies in a string. In this 1st solution, we used "count()" method along with iterations. The 2nd method doesn't deviate much from the core technique as it uses "Counter()" method along with iterations. We also discussed about the appending technique that defends the logic of frequency equalisation.