Group together all the anagrams [closed]
Problem statement: You are given a set of k strings, each length n. You have to output the group of anagrams together. Anagrams are like e.g atm - mat , like-kile.
Just sort the word's letters to obtain a signature that's anagram-specific. E.g., in Python,
sig = ''.join(sorted(word))
and make a dict
with sig
as the key and the value being a list of words with that signature (defaultdict(list)
works well for this). Of course, you can do it in any language with sorting abilities, and associative arrays whose values can be lists or vectors;-).
精彩评论