Python dictionary, set and counter to check if frequencies can become same

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.

Examples:

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,

  1. 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.
  2. Now extract list of frequencies of each character and push these values in Set() data structure in python.
  3. 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 ).
  4. filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # Function to Check if frequency of all characters
    # can become same by one removal
    from collections import Counter
      
    def allSame(input):
          
        # calculate frequency of each character
        # and convert string into dictionary
        dict=Counter(input)
      
        # now get list of all values and push it
        # in set
        same = set(dict.values())
      
        # now check if frequency of all characters 
        # can become same
        if (len(same)==1):
             print 'Yes'
        elif (len(same)==2):
             if (list(same)[0]==1):
                 print 'Yes'
             else:
                 print 'No'
      
    # Driver program
    if __name__ == "__main__":
        input = 'xxxxyyzz'
        allSame(input)

    chevron_right

    
    

    Output:

    No
    


    My Personal Notes arrow_drop_up

    Experienced Software Engineer with a demonstrated history of working in the information technology and services industry Skilled in Big Data technologies, Data Structures, Algorithm, Software Development, App Development, Databases, Python, Java, C, and WordPress

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.