This problem is asking us to construct the shortest possible string that contains every combination of n digits (drawn from 0 to k-1) as a contiguous substring. This is a classic problem in combinatorics known as a de Bruijn sequence.
With n = 2 and k = 2, there are four possible passwords: "00", "01", "10", "11". Concatenating them all gives "00011011" (length 8), but consecutive passwords can overlap: the end of one password can serve as the beginning of the next. "01100" achieves the same coverage in 5 characters by maximizing these overlaps.
This problem maps onto finding an Eulerian path in a graph where each password of length n is an edge and each string of length n-1 is a node. A path that traverses every edge exactly once spells out the shortest string containing all passwords.
1 <= n <= 4 and 1 <= k <= 10 → The total number of passwords is k^n, which is at most 4096. This is a small search space, so even approaches with higher constant factors will pass.1 <= k^n <= 4096 → The output string length is k^n + n - 1, which is at most 4099 characters. Any reasonable algorithm will work within time limits.Build the string greedily. Start with a string of n zeroes (the first password), then repeatedly extend it by one character so that the last n characters form a new, previously unseen password. This is a DFS on the de Bruijn graph without constructing the graph explicitly: the current node is the last n-1 characters, and each appended digit follows one edge.
If every extension leads to a password we have already seen, we backtrack: remove the character, unmark the password, and try a different digit one level up. The digit order in the code is deliberate. Starting from all zeroes and always trying the largest available digit first produces a complete de Bruijn sequence without ever reaching a dead end (a classical result due to Martin), so the backtracking branch is a safety net that never fires.
n zeroes. Mark this as a visited password.n-1 characters of the current string as the current suffix.k-1 down to 0 to form a candidate password of length n.k^n visited passwords, we have found our answer.Trace n = 2, k = 2. The string starts as "00" and digits are tried from k-1 down to 0:
Approach 1 avoids dead ends only because of its greedy digit order. The next approach uses the graph structure directly: the de Bruijn graph always contains an Eulerian circuit, and Hierholzer's algorithm constructs one with no backtracking machinery at all.
Model the problem as a directed graph where each node is a string of length n-1, and each edge is a password of length n connecting its prefix to its suffix. Each node has exactly k outgoing and k incoming edges, so the graph contains an Eulerian circuit. Hierholzer's algorithm finds this circuit with a DFS that collects edges in post-order: from each node, follow every unused edge, and append an edge's digit to the result only after the recursive call for that edge has fully finished. Appending the start node at the end completes the sequence.
Because every node has as many incoming edges as outgoing ones, a DFS that consumes one edge each time it moves can only run out of unused edges back at its starting node. Any edges still unvisited at that point form closed sub-circuits attached to nodes already on the path, and the post-order insertion splices those sub-circuits into the main circuit. The result visits every edge exactly once.
n = 1 by returning digits 0 through k-1 concatenated.n-1. Appending a digit d to node u forms the edge u + d, whose destination node is the last n-1 characters of the edge.n-1 zeroes.Trace n = 2, k = 2. The nodes are the single digits "0" and "1", and the four edges are the passwords "00", "01", "10", "11":