Working (but not efficient) version of the anagram algorithm