Last Updated: March 28, 2026
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.
Think about it this way: with n = 2 and k = 2, we have four possible passwords: "00", "01", "10", "11". We could naively concatenate them all to get "00011011" (length 8), but that is wasteful. Notice that consecutive passwords can overlap. The end of one password can serve as the beginning of the next. "01100" achieves the same coverage in just 5 characters by maximizing these overlaps.
The key observation is that this problem maps directly onto finding an Eulerian path in a specific graph. Each password of length n becomes an edge, and each prefix/suffix of length n-1 becomes a node. Finding a path that traverses every edge exactly once gives us 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.The most direct way to think about this: we need to build a string that contains every possible n-digit combination as a substring. Start with a string of n zeroes (that is our first password). Then repeatedly try to extend by one character such that the last n characters form a new, previously unseen password.
If we hit a dead end where all extensions lead to passwords we have already seen, we backtrack. The backtracking ensures we find a valid path through all k^n passwords. We are essentially doing a DFS on the de Bruijn graph, but without explicitly constructing the graph.
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.The backtracking approach works but may explore dead-end branches. Since the de Bruijn graph is guaranteed to have an Eulerian circuit, can we use Hierholzer's algorithm to find the path directly without any backtracking?
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 is Eulerian. Hierholzer's algorithm finds this circuit by doing a DFS with post-order edge collection: start at any node, follow edges greedily, and when stuck, append the digit to the result and backtrack through the recursion.
The de Bruijn graph is guaranteed to be Eulerian because every node has equal in-degree and out-degree (both equal to k). Hierholzer's algorithm exploits this: when you get "stuck" at a node with no unused edges, all remaining unvisited edges form sub-circuits. The post-order insertion weaves these sub-circuits into the main path, producing a valid Eulerian circuit that visits every edge exactly once.
n = 1 by returning digits 0 through k-1 concatenated.n-1, with k outgoing edges.n-1 zeroes.