Generate All Permutations of a String | Skilled.dev
Interview Question

Generate All Permutations

Write a function generatePermutations that finds all the of an input string.

  • If there are duplicate letters, you should include the duplicate permutations of each.
  • Time and space complexity is not considered for the quality of the solution — focus on readable code.
generatePermutations('dev')
// ['dev', 'dve', 'edv', 'evd', 'vde', 'ved']

// Notice you include duplicate permutations if there are duplicate letters
generatePermutations('bob')
// ['bob', 'bbo', 'bob', 'bbo', 'obb', 'obb']
Interactive Permutation Animation

Breakdown

Loading...

Table of Contents

Validate My Answer

  1. Our constraint says that the Big O complexity should not be factored into picking a solution. You will likely want to use recursion because the iterative solution is painfully long.

  2. Duplicate strings are allowed in the result if they form the same permutation. You only need to focus on creating the logic for the permutations without needing to validate.

  3. Any recursive solution must have a base case. Think through what it would be here and ensure it's included in your solution.

Loading...

Table of Contents

Test Results

Run your code and see results here...

const generatePermutations = (string) => {
  // Your solution here
};
// Upgrade for full course access
// Upgrade for full course access

Generate All Permutations

Write a function generatePermutations that finds all the of an input string.

  • If there are duplicate letters, you should include the duplicate permutations of each.
  • Time and space complexity is not considered for the quality of the solution — focus on readable code.
generatePermutations('dev')
// ['dev', 'dve', 'edv', 'evd', 'vde', 'ved']

// Notice you include duplicate permutations if there are duplicate letters
generatePermutations('bob')
// ['bob', 'bbo', 'bob', 'bbo', 'obb', 'obb']
const generatePermutations = (string) => {
  // Your solution here
};
// Upgrade for full course access
// Upgrade for full course access