I was asked this one when I applied for my current job.
views:
443answers:
8Sort each element (removing whitespace) and compare against the previous. If they are all the same, they're all anagrams.
Put all the letters in alphabetical order in the string (sorting algorithm) and then compare the resulting string.
For each word, compute a hash of the letters of that word (sorted however you like, probably ascending alphabetically). For each word with the same hash, they're an anagram.
Sort the letters and compare (letter by letter, string compare, ...) is the first things that comes to mind.
The following algorithm should work:
Sort the letters in each word.
Sort the sorted lists of letters in each list.
Compare each element in each list for equality.
Interestingly enough, Eric Lippert's Fabulous Adventures In Coding Blog dealt with a variation on this very problem on February 4, 2009 in this post.
Good thing we all live in the C# reality of in-place sorting of short words on quad core machines with oozles of memory. :-)
However, if you happen to be memory constrained and can't touch the original data and you know that those words contain characters from the lower half of the ASCII table, you could go for a different algorithm that counts the occurrence of each letter in each word instead of sorting.
You could also opt for that algorithm if you want to do it in O(N) and don't care about the memory usage (a counter for each Unicode char can be quite expensive).
- compare length (if not equal, not a chance)
- make a bit vector of the length of the strings
- for each
char
in the first string find occurrences of it in the second - set the bit for the first unset occurrence
- if you can find one stop with fail