Practice/Google/Leetcode 753. Cracking the Safe
CodingMust
You need to construct the shortest possible string that contains every possible password of length n using digits from 0 to k-1 as a contiguous substring. Each unique password must appear at least once within the final string.
For example, if n = 2 and k = 2, there are four possible passwords: "00", "01", "10", and "11". You need to find the shortest string where all four appear as substrings.
This problem relates to constructing what's known in graph theory as a special cyclic sequence where overlapping helps minimize total length.
0 to k-1n must appear as a contiguous substringExample 1:
Input: n = 1, k = 2 Output: "01" Explanation: All passwords of length 1 are "0" and "1". The string "01" contains both.
Example 2:
` Input: n = 2, k = 2 Output: "00110" Explanation: All passwords of length 2 are "00", "01", "10", "11". The string "00110" contains:
Example 3:
Input: n = 2, k = 3 Output: "0010211220" Explanation: This contains all 9 possible length-2 passwords using digits 0, 1, 2.