Python Counter to find the size of largest subset of anagram words

Given an array of n string containing lowercase letters. Find the size of largest subset of string which are anagram of each others. An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “abcd” and “dabc” are anagram of each other.


ant magenta magnate tan gnamate
Output: 3
Anagram strings(1) - ant, tan
Anagram strings(2) - magenta, magnate,
Thus, only second subset have largest
size i.e., 3

cars bikes arcs steer 
Output: 2

We have existing solution for this problem please refer Find the size of largest subset of anagram words link. We can solve this problem quickly in python using Counter() method. Approach is very simple,

  1. Split input string separated by space into words.
  2. As we know two strings are anagram to each other if they contain same character set. So to get all those strings together first we will sort each string in given list of strings.
  3. Now create a dictionary using Counter method having strings as keys and their frequencies as value.
  4. Check for maximum value of frequencies, that will be the largest sub-set of anagram strings.




# Function to find the size of largest subset 
# of anagram words
from collections import Counter
def maxAnagramSize(input):
    # split input string separated by space
    input = input.split(" ")
    # sort each string in given list of strings
    for i in range(0,len(input)):
    # now create dictionary using counter method
    # which will have strings as key and their 
    # frequencies as value
    freqDict = Counter(input)
    # get maximum value of frequency
    print (max(freqDict.values()))
# Driver program
if __name__ == "__main__":
    input = 'ant magenta magnate tan gnamate'




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. Strong engineering professional wi

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

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.