Skip to content

Commit 3a33f23

Browse files
Cracking The Safe
1 parent f791664 commit 3a33f23

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ My accepted leetcode solutions to some of the common interview problems.
110110
- [Number of Distinct Islands II](problems/src/depth_first_search/NumberOfDistinctIslandsII.java) (Hard)
111111
- [Smallest Rectangle Enclosing Black Pixels](problems/src/depth_first_search/SmallestRectangleEnclosingBlackPixels.java) (Hard)
112112
- [Bricks Falling When Hit](problems/src/depth_first_search/BricksFallingWhenHit.java) (Hard)
113-
- [Bricks Falling When Hit](problems/src/depth_first_search/RobotRoomCleaner.java) (Hard)
113+
- [Robot Room Cleaner](problems/src/depth_first_search/RobotRoomCleaner.java) (Hard)
114+
- [Cracking the Safe](problems/src/depth_first_search/CrackingTheSafe.java) (Hard)
114115

115116
#### [Design](problems/src/design)
116117

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package depth_first_search;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* Created by gouthamvidyapradhan on 09/03/2019
8+
* There is a box protected by a password. The password is n digits, where each letter can be one of the first k
9+
* digits 0, 1, ..., k-1.
10+
*
11+
* You can keep inputting the password, the password will automatically be matched against the last n digits entered.
12+
*
13+
* For example, assuming the password is "345", I can open it when I type "012345", but I enter a total of 6 digits.
14+
*
15+
* Please return any string of minimum length that is guaranteed to open the box after the entire string is inputted.
16+
*
17+
* Example 1:
18+
* Input: n = 1, k = 2
19+
* Output: "01"
20+
* Note: "10" will be accepted too.
21+
* Example 2:
22+
* Input: n = 2, k = 2
23+
* Output: "00110"
24+
* Note: "01100", "10011", "11001" will be accepted too.
25+
* Note:
26+
* n will be in the range [1, 4].
27+
* k will be in the range [1, 10].
28+
* k^n will be at most 4096.
29+
*
30+
* Solution O(n x k ^ n) Do a dfs and explore every possible states which form a n digit number with-in the given
31+
* range k. Maintain a 'result' string and keep appending the new digit in every state, if the total number of states
32+
* visited reaches k ^ n then, the result string will be the answer.
33+
*/
34+
public class CrackingTheSafe {
35+
36+
/**
37+
* Main method
38+
*
39+
* @param args
40+
*/
41+
public static void main(String[] args) {
42+
System.out.println(new CrackingTheSafe().crackSafe(4, 5));
43+
}
44+
45+
public String crackSafe(int n, int k) {
46+
int states = getStates(n, k);
47+
int[] N = new int[k];
48+
for (int i = 0; i < k; i++) {
49+
N[i] = i;
50+
}
51+
return generate(N, n, 0, 0, "", k, states);
52+
}
53+
54+
private int getStates(int n, int k) {
55+
if (n == 0) return 1;
56+
if (n == 1) return k;
57+
int result = 1;
58+
for (int i = 0; i < n; i++) {
59+
result *= k;
60+
}
61+
return result;
62+
}
63+
64+
private String generate(int[] N, int n, int i, int count, String num, int k, int states) {
65+
if (count == n) {
66+
return dfs(num, new StringBuilder(num), new HashSet<>(), k, states, 1);
67+
} else {
68+
for (int j = i; j < N.length; j++) {
69+
String result = generate(N, n, j, count + 1, num + String.valueOf(N[j]), k, states);
70+
if (!result.isEmpty()) {
71+
return result;
72+
}
73+
}
74+
}
75+
return "";
76+
}
77+
78+
private String dfs(String num, StringBuilder result, Set<String> done, int k, int states, int count) {
79+
done.add(num);
80+
if (states == count) {
81+
return result.toString();
82+
} else {
83+
for (int i = 0; i < k; i++) {
84+
String newNum = (num + String.valueOf(i));
85+
String newState = newNum.substring(1);
86+
if (!done.contains(newState)) {
87+
String retValue = dfs(newState, result.append(String.valueOf(i)), done, k, states, count + 1);
88+
if (!retValue.isEmpty()) {
89+
return retValue;
90+
} else {
91+
result.deleteCharAt(result.length() - 1);
92+
}
93+
}
94+
}
95+
}
96+
done.remove(num);
97+
return "";
98+
}
99+
}

0 commit comments

Comments
 (0)