Class Solution

java.lang.Object
g0701_0800.s0753_cracking_the_safe.Solution

public class Solution extends Object
753 - Cracking the Safe.<p>Hard</p> <p>There is a safe protected by a password. The password is a sequence of <code>n</code> digits where each digit can be in the range <code>[0, k - 1]</code>.</p> <p>The safe has a peculiar way of checking the password. When you enter in a sequence, it checks the <strong>most recent</strong> <code>n</code> <strong>digits</strong> that were entered each time you type a digit.</p> <ul> <li>For example, the correct password is <code>&quot;345&quot;</code> and you enter in <code>&quot;012345&quot;</code>: <ul> <li>After typing <code>0</code>, the most recent <code>3</code> digits is <code>&quot;0&quot;</code>, which is incorrect.</li> <li>After typing <code>1</code>, the most recent <code>3</code> digits is <code>&quot;01&quot;</code>, which is incorrect.</li> <li>After typing <code>2</code>, the most recent <code>3</code> digits is <code>&quot;012&quot;</code>, which is incorrect.</li> <li>After typing <code>3</code>, the most recent <code>3</code> digits is <code>&quot;123&quot;</code>, which is incorrect.</li> <li>After typing <code>4</code>, the most recent <code>3</code> digits is <code>&quot;234&quot;</code>, which is incorrect.</li> <li>After typing <code>5</code>, the most recent <code>3</code> digits is <code>&quot;345&quot;</code>, which is correct and the safe unlocks.</li> </ul> </li> </ul> <p>Return <em>any string of <strong>minimum length</strong> that will unlock the safe <strong>at some point</strong> of entering it</em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 1, k = 2</p> <p><strong>Output:</strong> &ldquo;10&rdquo;</p> <p><strong>Explanation:</strong> The password is a single digit, so enter each digit. &ldquo;01&rdquo; would also unlock the safe.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 2, k = 2</p> <p><strong>Output:</strong> &ldquo;01100&rdquo;</p> <p><strong>Explanation:</strong> For each possible password:</p> <ul> <li>&ldquo;00&rdquo; is typed in starting from the 4<sup>th</sup> digit.</li> <li>&ldquo;01&rdquo; is typed in starting from the 1<sup>st</sup> digit.</li> <li>&ldquo;10&rdquo; is typed in starting from the 3<sup>rd</sup> digit.</li> <li>&ldquo;11&rdquo; is typed in starting from the 2<sup>nd</sup> digit.</li> </ul> <p>Thus &ldquo;01100&rdquo; will unlock the safe. &ldquo;01100&rdquo;, &ldquo;10011&rdquo;, and &ldquo;11001&rdquo; would also unlock the safe.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 4</code></li> <li><code>1 <= k <= 10</code></li> <li><code>1 <= k<sup>n</sup> <= 4096</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • crackSafe

      public String crackSafe(int n, int k)