Question: Write a method to sort an array of strings so that all the anagrams are next to each other. a) Loop through the array. b)

Write a method to sort an array of strings so that all the anagrams are next to each other. 

a) Loop through the array.

b) For each word, sort the characters and add it to the hash map with keys as sorted word and value as the original word. At the end of the loop, you will get all anagrams as the value to a key (which is sorted by its constituent chars).

c) Iterate over the hashmap, print all values of a key together and then move to the next key.

Space Complexity: O(n), Time Complexity: O(n)

Step by Step Solution

3.46 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Answer To sort an array of strings so that all the anagrams are next to each other you can follo... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Data Structures Algorithms Questions!