Write a method to sort an array of strings so that all the anagrams are next to
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) 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)
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Related Book For
Problems Solving In Data Structures And Algorithms Using C++
ISBN: 9789356273177
2nd Edition
Authors: Hemant Jain
Question Posted: