Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time.
- Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
- You may assume no duplicates in the word list.
- You may assume beginWord and endWord are non-empty and are not the same.
Example 1:
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
Output: 5
Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Example 2:
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
Output: 0
Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
更新于 2019/09/28
又做了一遍,发现之前没有理解到位。
首先,这个题是一个bfs搜索问题
//bfs
public int ladderLength(String beginWord, String endWord, List<String> word_List) {
Set<String> wordList = new HashSet<>(word_List);
if(!wordList.contains(endWord)) return 0;
Set<String> beginSet = new HashSet<>();
beginSet.add(beginWord);
Set<String> seen = new HashSet<>();
//seen.add(beginWord);//不加也行 因为beginWord不在wordList中
int length = 1;
while(!beginSet.isEmpty()){
Set<String> newBeginSet = new HashSet<>();
for(String s:beginSet){
char[] array = s.toCharArray();
for(int i = 0;i<array.length;i++){
char temp = array[i];
for(char c = 'a';c<'z';c++){
array[i] = c;
String new_s = new String(array);
if(endWord.equals(new_s)){//注意用equals,而不是==
return length+1;
}
if(wordList.contains(new_s) && !seen.contains(new_s)){
newBeginSet.add(new_s);
seen.add(new_s);
}
}
array[i] = temp;
}
}
beginSet = newBeginSet;
length++;
}
return 0;
}
对于bfs问题,有一种双向搜索方法,可以加速
"The idea behind bidirectional search is to run two simultaneous searches—one forward from
the initial state and the other backward from the goal—hoping that the two searches meet in
the middle. The motivation is that b^(d/2) + b^(d/2) is much less than b^d. b is branch factor, d is depth. "
----- section 3.4.6 in Artificial Intelligence - A modern approach by Stuart Russel and Peter Norvig
于是就有了下面的方法:
//双向bfs
public int ladderLength(String beginWord, String endWord, List<String> word_List) {
Set<String> wordList = new HashSet<>(word_List);
if(!wordList.contains(endWord)) return 0;
Set<String> beginSet = new HashSet<>();
beginSet.add(beginWord);
Set<String> endSet = new HashSet<>();
endSet.add(endWord);
Set<String> seen = new HashSet<>();
//seen.add(beginWord);//不加也行 因为beginWord不在wordList中
int length = 1;
while(!beginSet.isEmpty()){
if(beginSet.size()>endSet.size()){
Set<String> tempSet = beginSet;
beginSet = endSet;
endSet = tempSet;
}
Set<String> newBeginSet = new HashSet<>();
for(String s:beginSet){
char[] array = s.toCharArray();
for(int i = 0;i<array.length;i++){
char temp = array[i];
for(char c = 'a';c<'z';c++){
array[i] = c;
String new_s = new String(array);
if(endSet.contains(new_s)){//这里则变成了检查是否在endSet里
return length+1;
}
if(wordList.contains(new_s) && !seen.contains(new_s)){
newBeginSet.add(new_s);
seen.add(new_s);
}
}
array[i] = temp;
}
}
beginSet = newBeginSet;
length++;
}
return 0;
}