Permutations

Try to solve the Permutations problem.

Statement#

Given an input string, word, return all possible permutations of the string.

Note: The order of permutations does not matter.

Constraints:

  • All characters in word are unique.

  • 11 \leq word.length 6\leq 6

  • All characters in word are lowercase English letters.

Created with Fabric.js 3.6.6

1 of 3

Created with Fabric.js 3.6.6

2 of 3

Created with Fabric.js 3.6.6

3 of 3

Understand the problem#

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Permutations

1

What should be the output if the following string is given as input?

Input string = “xyz”

A)

[“xyz”, “xzy”, “yxz”, “yzx”, “zyx”, “zxy”]

B)

[“xyz”, “xzy”, “xxz”, “yzx”, “zyx”, “zxz”]

C)

[“xyz”, “xzy”, “xxz”, “yzx”, “zyx”, “zxy”]

Question 1 of 30 attempted

Figure it out!#

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to rearrange them in the correct sequence.

Starting from the first index as the current index, recursively compute the permutations of the input string.

Compute the permutation by swapping the current index with every index in the remaining string.

Recurse the computation step by incrementing the current index by 1.

If we reach the end of the string, store the current string as a permutation.

Return the list of all permutations.


Try it yourself#

Implement your solution in the following coding playground:

Python
usercode > main.py
Input #1
Permutations

Subsets: Introduction

Solution: Permutations