Given a string array, return all groups of strings that are anagrams.
My solutions:
For each string word in the array, sort it O(m lg m), m is the average length of a word.
Build up a hash Table < string, list >.
Put the sorted word into the hash table as key and also generate all permutations (O(m!)) of the word, search each permutation in a dictionary (a prefix tree map) with O(m), if it is in the dictionary, put (O(1)) it into the hash table so that all permutated words are put into the list with the same key.
Totally, O(n * m * lg m * m!) time and O(n* m!) space , n is the size of the given array.
If m is very large, it is not efficient , m! .
Any better solutions ?
thanks