问题
给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:
每次转换只能改变一个字母。
转换过程中的中间单词必须是字典中的单词。
说明:
如果不存在这样的转换序列,返回 0。
所有单词具有相同的长度。
所有单词只由小写字母组成。
字典中不存在重复的单词。
你可以假设 beginWord 和 endWord 是非空的,且二者不相同。
示例 1:
输入:
beginWord = “hit”,
endWord = “cog”,
wordList = [“hot”,“dot”,“dog”,“lot”,“log”,“cog”]
输出: 5
解释: 一个最短转换序列是 “hit” -> “hot” -> “dot” -> “dog” -> “cog”,
返回它的长度 5。
示例 2:
输入:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,“dot”,“dog”,“lot”,“log”]
输出: 0
解释: endWord “cog” 不在字典中,所以无法进行转换。
分析
广度优先遍历;
两个解法:
1)当成一棵树来做,宽度优先遍历,借助队列;超时
2)应该还是相当于一个树?别人的解法,
解法一
count,记录树的深度;
首先beginWord入队;当队列不为空时,获得队头节点,将能通过队头结点变换一个字符得到的字符串加入队列(且这个字符存在于wordList中);如果变换一个字符得到的字符串 == endWord,则结束,输出count。
时间复杂度:O(n2*s),其中n是wordList的长度,s是单词的长度
对于wordList中的每个字符串,都要与wordList中的其他字符串比较,(比较时,又要判断每个字符是否相同)。
解法二
有点像一;
设置两个set,分别记录已得到的字符串,记为reached和未得到的字符串,记为wordSet(用wordList初始化wordSet);
初始时,已得到的字符串set中,只有beginWord;
当reached不包含endWord时,遍历reached
对于reached中的每个字符串str;遍历str的每一位字符,改变该字符,从a——z;判断变换后的字符串是否在wordSet中,如果在,则加入到一个临时的Set中,记为toAdd;(因为如果一边改变reached,一边遍历,会报异常)
遍历完一边reached后,如果toAdd为空,说明已经到达可以变换得到的字符串的边界;则return 0;
如果不为空,置 reached = toAdd,继续遍历reached。
(我基本上是把代码复述了一遍…… 我头太大了 就这样吧= =)
时间复杂度: O(26sn),其中s是单词长度;n是wordList长度。(??)not sure
对于wordList中的每个单词,都要对其每个字母进行26次变换;
代码
解法一
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
Queue<String> queue = new LinkedList<String>();
int count = 1; // 记录树的深度
queue.offer(beginWord);
int currCount = 1;
int nextCount = 0;
while (!queue.isEmpty()){
count++;
for(int i = 0; i < currCount; i++){
String curr = queue.poll(); //获得队头元素
boolean flag = false;
for (int j = 0; j < wordList.size(); j++ ){
String one = wordList.get(j);
if(isOneLetterDiffer(curr, one)){
nextCount++;
if(one.equals(endWord)){
flag = true;
break;
}else{
queue.offer(one);
}
}
}
if(flag){
return count;
}
}
if(count > wordList.size() + 1) return 0;
currCount = nextCount;
nextCount = 0;
}
return count;
}
private boolean isOneLetterDiffer(String str1, String str2){
int i = 0, j = 0;
int count = 0;
while(i < str1.length() && j < str2.length()){
if(str1.charAt(i) != str2.charAt(j)){
count++;
}
if (count > 1)
return false;
i++;j++;
}
return true;
}
解法二
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
if(!wordList.contains(endWord)) return 0;
Set<String> reachedSet = new HashSet<String>();
Set<String> wordSet = new HashSet<String>(wordList);
reachedSet.add(beginWord);
wordSet.add(endWord);
int count = 1;
while (!reachedSet.contains(endWord) ){
Set<String> toAdd = new HashSet<String>();
for (String str : reachedSet){
// 对于reachedSet中的每个单词,遍历其所有一个字符的变换 与wordSet比较 加入到toAdd中
int len = str.length();
for (int i = 0; i < len; i++){
char[] chArr = str.toCharArray();
// 对于str的每个字符
for(char ch = 'a'; ch <= 'z' ; ch++){
chArr[i] = ch;
String two = new String(chArr);
// 判断two是否在wordSet中
if(wordSet.contains(two)){
wordSet.remove(two);
toAdd.add(two);
}
}
}
}
if (toAdd.size() == 0) return 0;
count++;
reachedSet = toAdd;
}
if(!reachedSet.contains(endWord)) return 0;
return count;
}