...

/

Solution Review: Find All Permutations of a String

Solution Review: Find All Permutations of a String

In this lesson, we will review the solution to the challenge from the previous lesson.

We'll cover the following...

Solution #

Press + to interact
def permutations(str):
if str == "": # base case
return [""]
permutes = []
for char in str:
subpermutes = permutations(str.replace(char, "", 1)) # recursive step
for each in subpermutes:
permutes.append(char+each)
return permutes
def main():
print (permutations("abc"))
main()

Explanation

Just as we learned in previous lessons, the key to acing recursion is focusing on one step at a time. Try to think of this problem in this way: you already have every possible arrangement of every ...