Monday, July 18, 2011

Programming interview tip : Finding anagrams

Find all the words in a list of words (listA) which are anagrams of a single input word (wordA).

Bad solution (the trap): 
look at all the possible permutations of wordA and find them in listA.

This is a terrible solution and has a complexity of countOfCharactersInWord(wordA)!
where ! is the factorial symbol :)

Good solution:
take all the characters in wordA and sort them. Then compare it with the each item in listA first sorting the characters of that item as well :)

In python :
#the wordlist file is the SCOWL wordlist downloaded from : http://wordlist.sourceforge.net/
file = open("./SCOWL/wordlist.txt")
words =[line[:-1] for line in file.readlines()] #remove the newline character.
file.close()
#basic anagram comparion algorithm
def AreSameCharacters(word1,word2):
word1 = list(word1)
word2 = list(word2)
word1.sort()
word2.sort()
word1 = "".join(word1).lower()
word2 = "".join(word2).lower()
return word1==word2
def IsAnagramEqual(word1,word2):
if (word1==word2):
return False #we do not list the same word as an anagram :)
if len(word1) != len(word2):
return False
if not AreSameCharacters(word1,word2):
return False
return True
#word input by user
word1 = str(raw_input("Please Input Your Word: ")).strip().lower()
#single word comparisons:
for word2 in words:
if IsAnagramEqual(word1,word2):
print word2
view raw gistfile1.py hosted with ❤ by GitHub


This yields for my friend "omair" the following interesting (i made them bold) anagrams:
mario
arimo
moria
omari
maori
moira

I used the SCOWL wordlist for this. You can get the complete working code (including the wordlist) from here:
https://bitbucket.org/basarat/pyanagram/overview
Enjoy!

No comments:

Post a Comment