Skip to content

Instantly share code, notes, and snippets.

@vamsitallapudi
Last active February 13, 2020 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vamsitallapudi/c35cf525b8ef8429856e2e48782e65fe to your computer and use it in GitHub Desktop.
Save vamsitallapudi/c35cf525b8ef8429856e2e48782e65fe to your computer and use it in GitHub Desktop.
Print permutations of a given string using Python
def toStr(list):
return ''.join(list)
def permute(s, l, r):
"""
Function to find permutations of a given sting
:arg s : string,
:arg l : left element of the string,
:arg r : right element of the string.
"""
if l==r:
print(toStr(s))
for i in range(l, r):
s[i], s[l] = s[l], s[i]
permute(s, l + 1, r)
s[i], s[l] = s[l], s[i] # backtrack
s = input('Enter a string:')
n = len(s)
a = list(s)
permute(a, 0, n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment