Permutation Sequence

The set [1, 2, 3, ..., n] contains exactly n! unique permutations. When all permutations are listed in lexicographic order, they form a sequence ordered from smallest to largest.

Given two integers n and k, return the kth permutation sequence of the numbers from 1 to n, where k is 1-indexed.

Return the permutation as a string.

Example 1
Inputn = 3, k = 3
Output"213"
The permutations of [1, 2, 3] in order are "123", "132", "213", so the 3rd permutation is "213".
Example 2
Inputn = 4, k = 9
Output"2314"
The 9th permutation of [1, 2, 3, 4] in lexicographic order is "2314".

Constraints

  • 1 <= n <= 9
  • 1 <= k <= n!

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate