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.
- 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()
- Python Counter| Find all duplicate characters in string
- Dictionary and counter in Python to find winner of election
- Python Counter| Find duplicate rows in a binary matrix
- Find the k most frequent words from data set in Python
- Python | Find k longest words in given list
- Find size of a list in Python
- Python | Counter Objects | elements()
- Python Counter | Majority Element
- Python dictionary, set and counter to check if frequencies can become same
- Python | Minimum number of subsets with distinct elements using Counter
- Python program to find second largest number in a list
- Python program to find N largest elements from a list
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.