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.
Input: ant magenta magnate tan gnamate Output: 3 Explanation Anagram strings(1) - ant, tan Anagram strings(2) - magenta, magnate, gnamate Thus, only second subset have largest size i.e., 3 Input: 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,
- Split input string separated by space into words.
- 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.
- Now create a dictionary using Counter method having strings as keys and their frequencies as value.
- Check for maximum value of frequencies, that will be the largest sub-set of anagram strings.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python - Counter.items(), Counter.keys() and Counter.values()
- Find the size of largest subset of anagram words
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Anagram checking in Python using collections.Counter()
- SymPy | Subset.subset() in Python
- Python Counter| Find duplicate rows in a binary matrix
- Dictionary and counter in Python to find winner of election
- Python dictionary, set and counter to check if frequencies can become same
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python | Minimum number of subsets with distinct elements using Counter
- Python Counter | Majority Element
- Python | Counter Objects | elements()
- Operations on Python Counter
- Python | Case Counter in String
- Python - Occurrence counter in List of Records
- Python - Matrix elements Frequencies Counter
- Python - Case Insensitive string counter
- SymPy | Subset.size() in Python
- Python sorted() to check if two strings are anagram or not
- Python Dictionary | Check if binary representations of two numbers are anagram
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.