Skip to content

Instantly share code, notes, and snippets.

@jmuzsik
Last active March 8, 2018 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmuzsik/38cf910d620f14147307b65b52efffe7 to your computer and use it in GitHub Desktop.
Save jmuzsik/38cf910d620f14147307b65b52efffe7 to your computer and use it in GitHub Desktop.
def anagrams(s):
if s == "":
return [s]
else :
ans = []
for w in anagrams(s[1: ]):
for pos in range(len(w) + 1):
ans.append(w[: pos] + s[0] + w[pos: ])
return ans
anagrams("abc")
# returns ['abc', 'bac', 'bca', 'acb', 'cab', 'cba']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment