There is a safe protected by a password. The password is a sequence of n digits where each digit can be in the range [0, k - 1].
The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the most recent n digits that were entered each time you type a digit.
For example, the correct password is "345" and you enter in "012345":
0, the most recent 3 digits is "0", which is incorrect.1, the most recent 3 digits is "01", which is incorrect.2, the most recent 3 digits is "012", which is incorrect.3, the most recent 3 digits is "123", which is incorrect.4, the most recent 3 digits is "234", which is incorrect.5, the most recent 3 digits is "345", which is correct and the safe unlocks.Return any string of minimum length that will unlock the safe at some point of entering it.
Input: n = 1, k = 2
Output: "10"
Explanation: The password is a single digit, so enter each digit. "01" would also unlock the safe.
Input: n = 2, k = 2
Output: "01100"
Explanation: For each possible password:
- "00" is typed in starting from the 4th digit.
- "01" is typed in starting from the 1st digit.
- "10" is typed in starting from the 3rd digit.
- "11" is typed in starting from the 2nd digit.Thus "01100" will unlock the safe. "10011", and "11001" would also unlock the safe.
The problem can be seen as finding an Eulerian path in a de Bruijn graph. For a given n and k, the graph is constructed where each vertex is a string of length n-1, and edges are created by appending one more character from 0 to k-1. An Eulerian circuit exists if each vertex in a directed graph has equal in-degree and out-degree, which is the case here.
Here's the step-by-step intuition for this approach:
n-1.k characters (forming the next state).k possibilities and there are approximately k^(n-1) nodes to explore.In this approach, we attempt every possible combination while ensuring that the solution is of the minimum length. The backtracking will involve generating each possible path and checking if it could complete the cracking of the safe.