


An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase. We can generalize this in string processing by saying that an anagram of a string is another string with the same quantity of each character in it, in any order.
{ “abc”, “ged”, “dge”, “bac” }
In the above example the array should be divided into 2 groups. The first group consists of { “abc”, “bac” } and the second group consists of { “ged”, “dge” }.
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases are as follows.
The first line of each test case contains a single integer ‘N’ denoting the number of strings.
The next line contains 'N' single space-separated strings.
For each test case/query, print the anagrams belonging to the same group in a single line, where all the anagrams are separated by a single space, and each group will be printed in a separate line.
The output for every test case will be printed in a separate line.
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 1000
1 <= |STR_LIST[i]| <= 100
“STR_LIST[i]” contains only lowercase english letters.
Where ‘T’ denotes the number of test cases, ‘N’ denotes the number of strings, and |STR_LIST[i]| denotes the length of the i’th string.
Time Limit: 1 sec.
The idea behind this approach is that two or more than two strings are anagrams if and only if their sorted strings are equal. So we will use a HashMap, let’s say “anagramGroup”, where each key is a sorted string, and the key will be mapping to the list of indices from the given list of strings that form a group of anagrams. This means that if we sort the strings at those indices, we will get the sorted string that is the key for this list of indices in the hashmap.
The steps are as follows:
The key idea behind this approach is that we can transform each string into a string representing the character count. We will use an array “frequency", of size 26 such that each element of the array represents the number of a’s, b’s, c’s and so on… We will be using these frequencies to create a string, delimited by ‘#’ characters that we will use as a key for our HashMap.
For example :
str=”abbccc”, will be “#1#2#3#0#0#0#0…#0”, where there are 26 entries total with delimited by ‘#’.
Steps are as follows :
The idea is to sort each string and insert it into the trie. Each node of trie will have an array to store the indexes of the string ending at that node. Using a trie will improve the access time of the elements when compared to HashMap, hence it will be more efficient than using a HashMap.
The structure of the trie class will be:
class TrieNode
{
public:
TrieNode *children[26];
vector<int> index;
TrieNode()
{
for (int i = 0; i < 26; i++)
{
children[i] = NULL;
}
}
~TrieNode()
{
for (int i = 0; i < 26; i++)
{
if (children[i] != NULL)
{
delete children[i];
}
}
index.clear();
index.resize(0);
}
};The array “children” stores the pointers to the children nodes of the current node
The steps are as follows :