← Home

753. Cracking the Safe

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
graph traversalC++Markdown
753

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 753. Cracking the Safe, the solution in this repository is mainly a graph traversal solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: graph traversal, backtracking.

The notes already sitting in the source point us in the right direction:

  • backtracking
  • https://leetcode.com/problems/cracking-the-safe/discuss/153039/DFS-with-Explanations

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are backtrack, crackSafe.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • A set is doing the membership or uniqueness work, which keeps the main loop readable.
  • Substring checks are convenient but not free, so they are part of the real complexity story.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//backtracking
02//https://leetcode.com/problems/cracking-the-safe/discuss/153039/DFS-with-Explanations
03//Runtime: 12 ms, faster than 85.74% of C++ online submissions for Cracking the Safe.
04//Memory Usage: 10.2 MB, less than 40.89% of C++ online submissions for Cracking the Safe.
05class Solution {
06public:
07    int n, k, total;
08    
09    bool backtrack(string& ans, unordered_set<string>& visited){
10        if(visited.size() == total){
11            return true;
12        }else{
13            for(int i = 0; i < k; ++i){
14                ans.push_back('0'+i);
15                string cur = ans.substr(ans.size()-n);
16                if(visited.find(cur) == visited.end()){
17                    visited.insert(cur);
18                    if(backtrack(ans, visited)) return true;
19                    visited.erase(cur);
20                }
21                ans.pop_back();
22            }
23            return false;
24        }
25    }
26    
27    string crackSafe(int n, int k) {
28        this->n = n;
29        this->k = k;
30        total = pow(k, n);
31        
32        string ans(n, '0');
33        unordered_set<string> visited = {ans};
34        
35        backtrack(ans, visited);
36        
37        return ans;
38    }
39};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.