-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution10.java
More file actions
59 lines (55 loc) · 1.68 KB
/
Solution10.java
File metadata and controls
59 lines (55 loc) · 1.68 KB
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
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
/*
* Word Ladder
* start = "hit"
* end = "cog"
* dict = ["hot","dot","dog","lot","log"]
* shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog"
* return its length 5.
*/
public class Solution10 {
public int ladderLength(String start, String end, HashSet<String> dict) {
Set<String> set=new HashSet<String>();
Queue<String> queue=new LinkedList<String>();
queue.offer(start);
int distance=1;
int count=1;
set.add(start);
while(count>0){
while(count>0){
char[] curr=queue.poll().toCharArray();
for(int i=0; i<curr.length;i++){
char tmp=curr[i];
for(char c='a';c<='z';c++){
if(c==tmp) continue;
curr[i]=c;
String str=new String(curr);
if(str.equals(end)) return distance+1;
if(dict.contains(str) && !set.contains(str)){
queue.offer(str);
set.add(str);
}
}
curr[i]=tmp;
}
count--;
}
distance++;
count=queue.size();
}
return 0;
}
public static void main(String ...args) {
Solution10 s10 = new Solution10();
HashSet<String> dict = new HashSet<String>();
dict.add("hot");
dict.add("dot");
dict.add("dog");
dict.add("lot");
dict.add("log");
System.out.println(s10.ladderLength("hit", "hog", dict));
}
}