Given an array of words, print all anagrams together. For example, if the given array is {“cat”, “dog”, “tac”, “god”, “act”}, then output may be “cat tac act dog god”.
We have discussed two different methods in the previous post. In this post, a more efficient solution is discussed.
Trie data structure can be used for a more efficient solution. Insert the sorted order of each word in the trie. Since all the anagrams will end at the same leaf node. We can start a linked list at the leaf nodes where each node represents the index of the original array of words. Finally, traverse the Trie. While traversing the Trie, traverse each linked list one line at a time. Following are the detailed steps.
1) Create an empty Trie
2) One by one take all words of input sequence. Do following for each word
…a) Copy the word to a buffer.
…b) Sort the buffer
…c) Insert the sorted buffer and index of this word to Trie. Each leaf node of Trie is head of a Index list. The Index list stores index of words in original sequence. If sorted buffe is already present, we insert index of this word to the index list.
3) Traverse Trie. While traversing, if you reach a leaf node, traverse the index list. And print all words using the index obtained from Index list.
C++
// An efficient program to print all anagrams together #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define NO_OF_CHARS 26 // Structure to represent list node for indexes of words in // the given sequence. The list nodes are used to connect // anagrams at leaf nodes of Trie struct IndexNode { int index; struct IndexNode* next; }; // Structure to represent a Trie Node struct TrieNode { bool isEnd; // indicates end of word struct TrieNode* child[NO_OF_CHARS]; // 26 slots each for 'a' to 'z' struct IndexNode* head; // head of the index list }; // A utility function to create a new Trie node struct TrieNode* newTrieNode() { struct TrieNode* temp = new TrieNode; temp->isEnd = 0; temp->head = NULL; for ( int i = 0; i < NO_OF_CHARS; ++i) temp->child[i] = NULL; return temp; } /* Following function is needed for library function qsort(). Refer int compare( const void * a, const void * b) { return *( char *)a - *( char *)b; } /* A utility function to create a new linked list node */ struct IndexNode* newIndexNode( int index) { struct IndexNode* temp = new IndexNode; temp->index = index; temp->next = NULL; return temp; } // A utility function to insert a word to Trie void insert( struct TrieNode** root, char * word, int index) { // Base case if (*root == NULL) *root = newTrieNode(); if (*word != ' |