Ctrl K

Every Code in One Entry

Hard
A combination lock opens with a code of length digits, each digit from 0 to digits - 1. The lock needs no confirmation: after every digit you press, it compares the last length digits typed with the code and opens on a match. The code is unknown. Find a string of minimum length that is guaranteed to open the lock whatever the code is, meaning every possible code appears in it as a substring. There are usually several such strings. Return the one that is lexicographically smallest when read right to left: compare the last characters first, then the second to last ones, and so on.
Example 1
length=1, digits=3 → '210'

The code is a single digit from 0 to 2, so all three digits must be typed. Of the six such strings, "210" is the smallest when read from the end (backwards it is "012").

Example 2
length=2, digits=2 → '01100'

The shortest suitable strings have length 5: "00110", "01100", "10011", "11001". Read from the end they become "01100", "00110", "11001", "10011"; the smallest is "00110", so the answer is "01100".

Example 3
length=3, digits=1 → '000'
Constraints
  • 1 ≤ length ≤ 4
  • 1 ≤ digits ≤ 10
  • digits^length ≤ 4096
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRoute Through Every Boarding Pass HardA Chain of Domino Tiles HardLetter Order From a Dictionary HardLongest Uphill Walk HardBall Into the Hole Hard
loading Python…
Input
length=1, digits=3
Expected
'210'

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