Given a string which contains lower alphabetic characters, we need to remove at most one character from this string in such a way that frequency of each distinct character becomes same in the string.
Input : str = “xyyz” Output : Yes We can remove character ’y’ from above string to make the frequency of each character same. Input : str = “xyyzz” Output : Yes We can remove character ‘x’ from above string to make the frequency of each character same. Input : str = “xxxxyyzz” Output : No It is not possible to make frequency of each character same just by removing at most one character from above string.
This problem has existing solution please refer Check if frequency of all characters can become same by one removal link. We will solve this problem quickly in Python. Approach is very simple,
- We need to count frequency of each letter in string, for this we will use Counter(input) method, it returns a dictionary having characters as keys and their respective frequencies as values.
- Now extract list of frequencies of each character and push these values in Set() data structure in python.
- Since set contains unique values, so if size of set is 1 that means frequencies of all characters were same, if size of set is 2 then check if value of first element is 1 or not ( if 1 then we can make same frequency by removing one character at most otherwise not possible ).
- Python - Matrix elements Frequencies Counter
- Dictionary and counter in Python to find winner of election
- Python - Frequencies of Values in a Dictionary`
- Python - Associated Values Frequencies in Dictionary
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Counting the frequencies in a list using dictionary in Python
- Python | Check if key has Non-None value in dictionary
- Python | Check if dictionary is empty
- Python | Check if one dictionary is subset of other
- Python - Check for Key in Dictionary Value list
- Python | Check for None values in given dictionary
- Python | Check if all values are 0 in dictionary
- Python | Check whether given key already exists in a dictionary
- Python - Check if all values are K in dictionary
- Python | Check if tuple exists as dictionary key
- Python | Check if given multiple keys exist in a dictionary
- Python Dictionary | Check if binary representations of two numbers are anagram
- Check whether the frequencies of all the characters in a string are prime or not
- Python - Filter dictionary values in heterogenous dictionary
- Python | Convert flattened dictionary into nested dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : go__pal