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.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").
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".
length=3, digits=1 → '000'
length=1, digits=3
'210'
Run checks the first 3 tests in your browser. Submit checks all 22 tests on the server, hidden ones included.