Skip to content

Instantly share code, notes, and snippets.

@EdisonChendi
Created July 29, 2018 13:07
Show Gist options
  • Save EdisonChendi/6ad8f77d4202177684b2cea8b9e81538 to your computer and use it in GitHub Desktop.
Save EdisonChendi/6ad8f77d4202177684b2cea8b9e81538 to your computer and use it in GitHub Desktop.
find all anagrams in a sentence
#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