# 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.

Input:ant magenta magnate tan gnamateOutput:3ExplanationAnagram strings(1) - ant, tan Anagram strings(2) - magenta, magnate, gnamate Thus, only second subset have largest size i.e., 3Input:cars bikes arcs steerOutput:2

**Naive appraoch** is to generate all possible subset and iterate from largest size of subset containing all string having same size and anagram of each others. Time complexity of this appraoch is O() where n and m are the size of array and length of string respectively.

**Efficient approach** is to use hashing and sorting. Sort all characters of string and store the hash value(sorted string) in map(unordered_map in C++ and HashMap in java). At last check which one is the frequency sorted word with the highest number of occurrence.

`// C++ Program to find the size of ` `// largest subset of anagram ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Utility function to find size of ` `// largest subset of anagram ` `int` `largestAnagramSet(string arr[], ` `int` `n) ` `{ ` ` ` ` ` `int` `maxSize = 0; ` ` ` `unordered_map<string, ` `int` `> count; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` ` ` `// sort the string ` ` ` `sort(arr[i].begin(), arr[i].end()); ` ` ` ` ` `// Increment the count of string ` ` ` `count[arr[i]] += 1; ` ` ` ` ` `// Compute the maximum size of string ` ` ` `maxSize = max(maxSize, count[arr[i]]); ` ` ` `} ` ` ` ` ` `return` `maxSize; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string arr[] = { ` `"ant"` `, ` `"magenta"` `, ` ` ` `"magnate"` `, ` `"tan"` `, ` `"gnamate"` `}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << largestAnagramSet(arr, n) << ` `"\n"` `; ` ` ` ` ` `string arr1[] = { ` `"cars"` `, ` `"bikes"` `, ` `"arcs"` `, ` ` ` `"steer"` `}; ` ` ` `n = ` `sizeof` `(arr1) / ` `sizeof` `(arr[0]); ` ` ` `cout << largestAnagramSet(arr1, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output3 2

**Time complexity: **O() where m is maximum size among all of the strings

**Auxiliary space: **O(n + m)

**Best approach** is to store the frequency array of each word. In this, we just need to iterate over the characters of the words and increment the frequency of current letter. At last, increment the count of only identical frequency array[] and take the maximum among them. **This approach is best only when length of strings are maximum in comparison to the array size**.

`// C++ Program to find the size of ` `// largest subset of anagram ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Utility function to find size of ` `// largest subset of anagram ` `int` `largestAnagramSet(string arr[], ` `int` `n) ` `{ ` ` ` `int` `maxSize = 0; ` ` ` ` ` `// Initialize map<> of vector array ` ` ` `map<vector<` `int` `>, ` `int` `> count; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` ` ` `// Vector array to store ` ` ` `// frequency of element ` ` ` `vector<` `int` `> freq(26); ` ` ` ` ` `for` `(` `char` `ch : arr[i]) ` ` ` `freq[ch - ` `'a'` `] += 1; ` ` ` ` ` `// Increment the count of ` ` ` `// frequency array in map<> ` ` ` `count[freq] += 1; ` ` ` ` ` `// Compute the maximum size ` ` ` `maxSize = max(maxSize, count[freq]); ` ` ` `} ` ` ` `return` `maxSize; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string arr[] = { ` `"ant"` `, ` `"magenta"` `, ` `"magnate"` `, ` ` ` `"tan"` `, ` `"gnamate"` `}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << largestAnagramSet(arr, n) << ` `"\n"` `; ` ` ` ` ` `string arr1[] = { ` `"cars"` `, ` `"bikes"` `, ` `"arcs"` `, ` ` ` `"steer"` `}; ` ` ` `n = ` `sizeof` `(arr1) / ` `sizeof` `(arr[0]); ` ` ` `cout << largestAnagramSet(arr1, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output3 2

**Time complexity: **O() where m is maximum size among all of the strings

**Auxiliary space: **O(n + m)

## Recommended Posts:

- Python Counter to find the size of largest subset of anagram words
- Largest subset with maximum difference as 1
- Lexicographically smallest and largest substring of size k
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Check if the given string of words can be formed from words present in the dictionary
- Find words which are greater than given length k
- Find words which are greater than given length k using stringstream
- Python program to find uncommon words from two Strings
- Find alphabetical order such that words can be considered sorted
- Count words that appear exactly two times in an array of words
- Find the starting indices of the substrings in string (S) which is made by concatenating all words from a list(L)
- Find largest d in array such that a + b + c = d
- Find the k largest numbers after deleting the given elements
- Find the lexicographically largest palindromic Subsequence of a String
- Program to find Smallest and Largest Word in a String

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.