Back to Practice
#0063
Generate Permutations
MediumDSA30 min20 XP
Problem
Given a list of distinct values, return a list containing all permutations. The output order does not matter.
Why This Matters
Backtracking is controlled trial and undo. This problem is the cleanest way to understand choose, explore, unchoose.
Function Signature
def permutations(items):
Examples
Example 1
Input[1, 2, 3]
Outputsix permutations
There are 3 choices for the first position, then 2, then 1.
Constraints
- Items are distinct.
- Return [[]] for an empty input.
- Output order does not matter.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Three items
Input[1, 2, 3]
Expected6 permutations
3 factorial equals 6.
Hidden Test Categories
Empty inputOne itemString values