Skip to content

Instantly share code, notes, and snippets.

@CleverProgrammer
Last active August 31, 2015 08:08
Show Gist options
  • Save CleverProgrammer/9183c4b587b622787e1a to your computer and use it in GitHub Desktop.
Save CleverProgrammer/9183c4b587b622787e1a to your computer and use it in GitHub Desktop.
anagram.py
# coding: utf-8
words = ['cat','dog','tac','star','rats']
def anagram(str1,str2):
s1 = set(str1)
s2 = set(str2) # ==> sorted(list(str2)) less efficient!
return s1 == s2 # ==> if s1==s2: return True else: return False
#print(anagram_solver('cpa','tac'))
def anagram_solver(words):
res = []
for word in words:
for another_word in words:
if another_word != word and anagram(word, another_word):
res.append(word)
return sorted(list(set(res)))
print(anagram_solver(words))
# O(N^2) --> Current algorithm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment