forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLadderLength_1218_2014.java
73 lines (57 loc) · 2.14 KB
/
LadderLength_1218_2014.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package Algorithms.bfs;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
public class LadderLength_1218_2014 {
public static void main(String[] strs) {
HashSet<String> set = new HashSet<String>();
set.add("hot");
set.add("dot");
set.add("dog");
set.add("lot");
set.add("log");
set.add("cog");
System.out.println(ladderLength("hit", "cog", set));
}
public static int ladderLength(String start, String end, Set<String> dict) {
if (start == null || end == null || dict == null) {
return 0;
}
// Bug 1: quese is a interface not a class
Queue<String> q = new LinkedList<String>();
q.offer(start);
HashSet<String> set = new HashSet<String>();
set.add(start);
// Bug 3: lever start from 1;
int level = 1;
while (!q.isEmpty()) {
int size = q.size();
level++;
for (int i = 0; i < size; i++) {
String s = q.poll();
int len = s.length();
for (int j = 0; j < len; j++) {
StringBuilder sb = new StringBuilder(s);
for (char c = 'a'; c <= 'z'; c++) {
// Bug 2: setCharAt
sb.setCharAt(j, c);
String tmp = sb.toString();
// Should be in the dict and not in the hashset.
if (set.contains(tmp) || !dict.contains(tmp)) {
continue;
}
if (tmp.equals(end)) {
return level;
}
set.add(tmp);
q.offer(tmp);
}
}
}
}
// When not found, return 0;
// "hot", "dog", ["hot","dog"]
return 0;
}
}