leetcode 127. Word Ladder
2018-03-04 15:35
393 查看
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord toendWord, 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.
For example,Given:
beginWord =
endWord =
wordList =
As one shortest transformation is
return its length
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.
UPDATE (2017/1/20):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.
给出一个beginWord,一个endWord,每次通过wordList做一次转换,把beginWord转换成endWord,求整个转换链的长度。
每次转换只能换一个字符。
由于转换的路径不止一条,通过深度优先搜索得到的路径可能不是最佳方案,不大容易得到正确答案。
使用广度优先遍历,每次从当前节点出发,搜索出所有可以转换的字符串(即进行一层广度搜索),此时转换链的长度+1;
一次搜索下去,当搜到endWord时,搜索的层数+1 即为转换链的长度。
看到有个博客对此方法的解释比较清晰:(https://www.jianshu.com/p/753bd585d57e)
![](https://upload-images.jianshu.io/upload_images/869170-8f0d22a7b8588100.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/585)
![](https://upload-images.jianshu.io/upload_images/869170-c490ec68148f6f16.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](https://upload-images.jianshu.io/upload_images/869170-0d205dd053af3256.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](https://upload-images.jianshu.io/upload_images/869170-1f2bd3aaee3d457b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](https://upload-images.jianshu.io/upload_images/869170-94b77120af02de8f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](https://upload-images.jianshu.io/upload_images/869170-4c079cdd51af4ff9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
搜索到新一层时,得到的方案可能有多个,比如hot-lot,hot-dot,这时应该都视作下一层的搜索起点,才能保证每一层的搜索是完整的
AC代码: public int ladderLength(String beginWord, String endWord, List<String> wordList) {
if(!wordList.contains(endWord)){
return 0;
}
if(beginWord.equals(endWord)){
return 0;
}
if(compareTwoStr(beginWord,endWord)){
return 2;
}
List<String> visited = new ArrayList<String>();
List<String> remain = new ArrayList<String>(wordList);
visited.add(beginWord);
int index = 0;
int visitLen = 1;
int count = 1;
while(!visited.contains(endWord)){
boolean found = false;
for(int i=index;i<index+visitLen;i++){
for(int j=remain.size()-1 ;j>=0; j--){
if(compareTwoStr(remain.get(j), visited.get(i))
&& !visited.contains(remain.get(j))){
visited.add(remain.get(j));
found = true;
remain.remove(j);
}
}
}
if(!found){
return 0;
}
count++;
index += visitLen;
visitLen = visited.size() - index;
}
return count;
}
//辅助函数,判断两串只相差1字符
public static boolean compareTwoStr(String str1,String str2){
int count = 0;
for(int i=0;i<str1.length();i++){
if(str1.charAt(i) != str2.charAt(i)){
count++;
if(count>1)
return false;
}
}
return count == 1;
}
Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
For example,Given:
beginWord =
"hit"
endWord =
"cog"
wordList =
["hot","dot","dog","lot","log","cog"]
As one shortest transformation is
"hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length
5.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.
UPDATE (2017/1/20):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.
给出一个beginWord,一个endWord,每次通过wordList做一次转换,把beginWord转换成endWord,求整个转换链的长度。
每次转换只能换一个字符。
由于转换的路径不止一条,通过深度优先搜索得到的路径可能不是最佳方案,不大容易得到正确答案。
使用广度优先遍历,每次从当前节点出发,搜索出所有可以转换的字符串(即进行一层广度搜索),此时转换链的长度+1;
一次搜索下去,当搜到endWord时,搜索的层数+1 即为转换链的长度。
看到有个博客对此方法的解释比较清晰:(https://www.jianshu.com/p/753bd585d57e)
![](https://upload-images.jianshu.io/upload_images/869170-8f0d22a7b8588100.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/585)
![](https://upload-images.jianshu.io/upload_images/869170-c490ec68148f6f16.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](https://upload-images.jianshu.io/upload_images/869170-0d205dd053af3256.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](https://upload-images.jianshu.io/upload_images/869170-1f2bd3aaee3d457b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](https://upload-images.jianshu.io/upload_images/869170-94b77120af02de8f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
![](https://upload-images.jianshu.io/upload_images/869170-4c079cdd51af4ff9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/700)
搜索到新一层时,得到的方案可能有多个,比如hot-lot,hot-dot,这时应该都视作下一层的搜索起点,才能保证每一层的搜索是完整的
AC代码: public int ladderLength(String beginWord, String endWord, List<String> wordList) {
if(!wordList.contains(endWord)){
return 0;
}
if(beginWord.equals(endWord)){
return 0;
}
if(compareTwoStr(beginWord,endWord)){
return 2;
}
List<String> visited = new ArrayList<String>();
List<String> remain = new ArrayList<String>(wordList);
visited.add(beginWord);
int index = 0;
int visitLen = 1;
int count = 1;
while(!visited.contains(endWord)){
boolean found = false;
for(int i=index;i<index+visitLen;i++){
for(int j=remain.size()-1 ;j>=0; j--){
if(compareTwoStr(remain.get(j), visited.get(i))
&& !visited.contains(remain.get(j))){
visited.add(remain.get(j));
found = true;
remain.remove(j);
}
}
}
if(!found){
return 0;
}
count++;
index += visitLen;
visitLen = visited.size() - index;
}
return count;
}
//辅助函数,判断两串只相差1字符
public static boolean compareTwoStr(String str1,String str2){
int count = 0;
for(int i=0;i<str1.length();i++){
if(str1.charAt(i) != str2.charAt(i)){
count++;
if(count>1)
return false;
}
}
return count == 1;
}
相关文章推荐
- 第六周:[Leetcode]127. Word Ladder
- leetcode 127 —— Word Ladder
- [LeetCode#127]Word Ladder
- [LeetCode]题解(python):127-Word Ladder
- [LeetCode] 127. Word Ladder
- LeetCode127—Word Ladder
- Leetcode 127. Word Ladder
- LeetCode 127 Word Ladder
- [LeetCode 127] Word Ladder
- LeetCode 127 Word Ladder
- leetcode 127. Word Ladder
- leetcode[127]Word Ladder
- [LeetCode]127 Word Ladder
- Leetcode 127. Word Ladder
- Leetcode#127 Word Ladder
- Leetcode 127 Word Ladder
- [Leetcode] 127. Word Ladder
- [leetcode]127. Word Ladder
- Java for LeetCode 127 Word Ladder
- LeetCode 127. Word Ladder