Detect Anagrams
excerpt: This covers the building block, Word Signature tags: wordsignature
A word w is an anagram of a word v if a permutation of the letters transforming w into v exists. Given a set of n words of length at most k, detect all possible anagrams.
Input
below the car is a rat drinking cider and bending its elbow while this thing is an arc that can act like a cat which cried during the night caused by pain in its bowel
Output
{bowel below elbow}, {arc car}, {night thing}, {cried cider}, {act cat}
Solution
Compute a signature for each word, so that two words are anagrams of each other if and only if they have the same signature. This signature is simply a new string made up of the same letters sorted in lexicographical order. The data structure used is a dictionary associating with each signature the list of words with this signature.
Implementation


This prints:
[["below", "elbow", "bowel"], ["car", "arc"], ["cider", "cried"], ["thing", "night"], ["act", "cat"]]
Complexity Analysis
The time complexity is O(n*k logk) on average and O(n2k log k) in the worst case, due to the use of a dictionary.