Cracking the Safe LeetCode solution in C++

1
class Solution {
2
public:
3
int total;
4
unordered_set<string> st;
5
bool func(int i, int k, int n, string &s) {
6
if (i == total) return 1;
7

8
for (int j = 0; j < k; j++) {
9
s.push_back(j + '0');
10

11
if (s.size() >= n) {
12
string a = s.substr(s.size() - n);
13
if (st.find(a) == st.end()) {
14
st.insert(a);
15
if (func(i + 1, k, n, s)) return 1;
16
st.erase(a);
17
}
18
} else if (func(i, k, n, s))
19
return 1;
20

21
s.pop_back();
22
}
23

24
return 0;
25
}
26
string crackSafe(int n, int k) {
27
total = pow(k, n);
28
string s;
29
func(0, k, n, s);
30
return s;
31
}
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0