752 Open the Lock

1. Question

You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'. The wheels can rotate freely and wrap around: for example we can turn'9'to be'0', or'0'to be'9'. Each move consists of turning one wheel one slot.
The lock initially starts at'0000', a string representing the state of the 4 wheels.
You are given a list ofdeadendsdead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it.
Given atargetrepresenting the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible.
Example 1:
1
Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202"
2
3
Output: 6
4
5
Explanation:
6
A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" ->
7
"1201" -> "1202" -> "0202".
8
Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid,
9
because the wheels of the lock become stuck after the display becomes the dead end "0102".
Copied!
Example 2:
1
Input: deadends = ["8888"], target = "0009"
2
3
Output: 1
4
5
Explanation:
6
7
We can turn the last wheel in reverse to move from "0000" -> "0009".
Copied!
Example 3:
1
Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888"
2
3
Output: -1
4
5
Explanation: We can't reach the target without getting stuck.
Copied!
Example 4:
1
Input: deadends = ["0000"], target = "8888"
2
3
Output: -1
Copied!
Note:
  1. 1.
    The length ofdeadendswill be in the range[1, 500].
  2. 2.
    targetwill not be in the listdeadends.
  3. 3.
    Every string indeadendsand the stringtargetwill be a string of 4 digits from the 10,000 possibilities'0000'
    to'9999'.

2. Implementation

(1) BFS
1
class Solution {
2
public int openLock(String[] deadends, String target) {
3
Set<String> deads = new HashSet<>(Arrays.asList(deadends));
4
Queue<String> queue = new LinkedList<>();
5
Set<String> visited = new HashSet<>();
6
7
queue.add("0000");
8
visited.add("0000");
9
10
int level = 0;
11
12
while (!queue.isEmpty()) {
13
int size = queue.size();
14
15
for (int i = 0; i < size; i++) {
16
String curCode = queue.remove();
17
18
if (deads.contains(curCode)) {
19
continue;
20
}
21
else if (curCode.equals(target)) {
22
return level;
23
}
24
else {
25
StringBuilder sb = new StringBuilder(curCode);
26
for (int j = 0; j < 4; j++) {
27
char c = sb.charAt(j);
28
29
String nextCode1 = sb.substring(0, j) + (c == '9' ? 0 : c - '0' + 1) + sb.substring(j + 1);
30
String nextCode2 = sb.substring(0, j) + (c == '0' ? 9 : c - '0' - 1) + sb.substring(j + 1);
31
32
if (!visited.contains(nextCode1) && !deads.contains(nextCode1)) {
33
visited.add(nextCode1);
34
queue.add(nextCode1);
35
}
36
37
if (!visited.contains(nextCode2) && !deads.contains(nextCode2)) {
38
visited.add(nextCode2);
39
queue.add(nextCode2);
40
}
41
}
42
}
43
}
44
++level;
45
}
46
return -1;
47
}
48
}
Copied!
(2) Bi-directional BFS
1
class Solution {
2
public int openLock(String[] deadends, String target) {
3
Set<String> deads = new HashSet<>(Arrays.asList(deadends));
4
Set<String> begin = new HashSet<>();
5
Set<String> end = new HashSet<>();
6
7
begin.add("0000");
8
end.add(target);
9
10
int level = 0;
11
12
while (!begin.isEmpty() && !end.isEmpty()) {
13
// Always start with a smaller set
14
if (begin.size() > end.size()) {
15
Set<String> set = begin;
16
begin = end;
17
end = set;
18
}
19
Set<String> temp = new HashSet<>();
20
for (String code : begin) {
21
if (deads.contains(code)) {
22
continue;
23
}
24
25
26
for (int i = 0; i < code.length(); i++) {
27
char c = code.charAt(i);
28
String nextCode1 = code.substring(0, i) + (c == '9' ? 0 : c - '0' + 1) + code.substring(i + 1);
29
String nextCode2 = code.substring(0, i) + (c == '0' ? 9 : c - '0' - 1) + code.substring(i + 1);
30
31
if (end.contains(nextCode1) || end.contains(nextCode2)) {
32
return level + 1;
33
}
34
35
if (!deads.contains(nextCode1)) {
36
temp.add(nextCode1);
37
}
38
39
if (!deads.contains(nextCode2)) {
40
temp.add(nextCode2);
41
}
42
}
43
}
44
++level;
45
begin = temp;
46
}
47
return -1;
48
}
49
}
Copied!

3. Time & Space Complexity

BFS: 时间复杂度O(m ^ n + d), m是在转轮上digit的个数(10 in this case), n是lock的digit个数(4 in this case), d是deadends的size, 空间复杂度O(m ^ n + d)
Bi-directional BFS: 时间复杂度O(m ^ n + d), m是在转轮上digit的个数(10 in this case), n是lock的digit个数(4 in this case), d是deadends的size, 空间复杂度O(m ^ n + d)