Given an array of strings strs, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Solution
Use a hash table to store the anagrams. For each string, sort the characters and use the sorted string as a key in the hash table. Add the original string to the array of anagrams for that key. Finally, return the values of the hash table as an array.
Add the string to the array of anagrams for the key.
Return the values of the map as an array.
Complexity Analysis
The time complexity of this algorithm is O(n _ m _ log(m)), where n is the number of strings and m is the length of the longest string. This is because we are sorting each string.
The space complexity of this algorithm is O(n * m), where n is the number of strings and m is the length of the longest string. This is because we are storing the anagrams in a map.