Last active
August 31, 2015 08:08
-
-
Save CleverProgrammer/9183c4b587b622787e1a to your computer and use it in GitHub Desktop.
anagram.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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