Make delicious recipes!

Find anagrams in a group of words

Two words are said to be anagrams of each other if they are made up of same characters.

If we have an array of words, then we want to group the words of one anagram together.

For example:

If input array is [“ram”, “pot”, “mar”, “top”, “arm”]

Then output array should be [“ram”, “mar”, “arm”, “pot”, “top”]

A simple solution for this is to sort each word and put in a hash-map.

Another solution is to sort each word and put in a trie.

If while putting in hash-map or trie, the word already exists, then the new word is an anagram of the existing word and should be grouped together with it.

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal