15% off all plans & roadmaps Use code
See plansLeetCode 60 · Solution & editorial
The set [1, 2, 3, ..., n] contains a total of n! unique permutations.
Permutation Sequence is a Backtracking Patterns problem, in the Permutations subpattern. The pattern covers subsets, permutations, combinations, N-Queens, and word search
Generate all permutations using backtracking. Handle duplicates and next permutation.
The reference solution runs in O(n^2) time and O(n) space.
Permutation Sequence has been reported in interviews at Amazon, Adobe.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Permutation Sequence is a Backtracking Patterns problem, in the Permutations subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.