I apologize that there have already been many posts about this problem. However, I am having a hard time seeing where I am going wrong in my own implementation. So I am trying to write a function that takes in a string and returns all of it's possible permutations in the form of a list.
Theoretically it should look like this:
allPermutations("abc...z") = [a + allPermutations(b,c,...z), b + allPermutations(a,c...z)...]
My current implementation, when tested on the String, "Hello", outputs a bunch of duplicate permutations. Can anyone help me see where I am going wrong. I appreciate your assistance!
Here is the Code:
def allPermutations(strng):
if len(strng) ==1:
return [strng]
perm_list = []
for i in strng:
smallerStr = strng.replace(i,"",1)
z = allPermutations(smallerStr)
for t in z:
perm_list.append(i+t)
return perm_list