Last active
February 13, 2020 16:22
-
-
Save vamsitallapudi/c35cf525b8ef8429856e2e48782e65fe to your computer and use it in GitHub Desktop.
Print permutations of a given string using Python
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
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