-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path127. Word Ladder.java
More file actions
33 lines (32 loc) · 1.36 KB
/
Copy path127. Word Ladder.java
File metadata and controls
33 lines (32 loc) · 1.36 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
public class Solution {
public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
Queue<String> queue=new LinkedList<>();
queue.offer(beginWord);
/* store the string can access to other strings */
Map<String,Set<String>> access=new HashMap<>();
/* store the steps to arrive the string */
Map<String,Integer> step=new HashMap<>();
step.put(beginWord,1);
while(!queue.isEmpty()){
String temp=queue.poll();
for(int i=0;i<temp.length();i++){
StringBuilder sb=new StringBuilder(temp);
for (char alpha='a';alpha<='z';alpha++){
if(alpha==temp.charAt(i)) continue;
sb.setCharAt(i,alpha);
if(wordList.contains(sb.toString())){
queue.offer(sb.toString());
if(step.containsKey(sb.toString())){
step.put(sb.toString(),Math.min(step.get(sb.toString()),step.get(temp)+1));
}
else {
step.put(sb.toString(),step.get(temp)+1);
}
if(sb.toString().equals(endWord)) return step.get(endWord);
}
}
}
}
return 0;
}
}