Ctrl K

The K-th Ordering of Digits

Hard
The digits 1 through size can be written as a string in many orders, size! of them in total. Sort all those strings in increasing order, the way words are sorted in a dictionary. Return the string that sits at position rank in that list (counting from one). There is no need to list every ordering: try to build the answer digit by digit.
Example 1
size=3, rank=4 → '231'

The orderings of 1, 2, 3 in increasing order are 123, 132, 213, 231, 312, 321. The fourth one is "231".

Example 2
size=4, rank=14 → '3142'

The first 6 orderings start with 1 and the next 6 with 2, so the 14th starts with 3 and is the second in its group: "3142".

Example 3
size=1, rank=1 → '1'
Constraints
  • 1 ≤ size ≤ 9
  • 1 ≤ rank ≤ size!
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPlus-Minus Calculator With Brackets HardHow Many Ones Are Written HardAmount in Words HardSimplify a Letter Polynomial HardCalculator With Brackets and Four Operations Hard
loading Python…
Input
size=3, rank=4
Expected
'231'

Run checks the first 3 tests in your browser. Submit checks all 59 tests on the server, hidden ones included.