Created
July 29, 2018 13:07
-
-
Save EdisonChendi/6ad8f77d4202177684b2cea8b9e81538 to your computer and use it in GitHub Desktop.
find all anagrams in a sentence
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 | |
from collections import defaultdict | |
def find_anagrams(setence): | |
words = set(setence.split()) | |
dd = defaultdict(list) | |
for w in words: # O(nklgk) | |
dd["".join(sorted(w))].append(w) | |
return {k:v for k, v in dd.items() if len(v) > 1} # O(n) | |
test_case = "le chien marche vers sa niche et trouve une limace de chine nue pleine de malice qui lui fait du charme" | |
print(find_anagrams(test_case)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment