描述
给出两个单词(start和end)和一个字典,找到从start到end的最短转换序列
比如:
1.每次只能改变一个字母。
2.变换过程中的中间单词必须在字典中出现。
注意事项:
如果没有转换序列则返回0。
所有单词具有相同的长度。
所有单词都只包含小写字母。
样例
给出数据如下:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
一个最短的变换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog",
返回它的长度 5
思路
这种题型叫隐式图搜索,题的思路就是在dict中找到和start单词相差一个字母的单词,一个接一个找,将单词当做结点,在结点构成的图中使用BFS
代码
- LintCode ( Set<String> )
public class Solution {
/**
* @param start, a string
* @param end, a string
* @param dict, a set of string
* @return an integer
*/
public int ladderLength(String start, String end, Set<String> dict) {
if (dict == null) {
return 0;
}
// start和end是两个单词,最少也要经过一步转化
if (start.equals(end)) {
return 1;
}
HashSet<String> hash = new HashSet<>();
Queue<String> queue = new LinkedList<>();
queue.offer(start);
hash.add(start);
dict.add(end);
// 异常情况都考虑了,所以一旦进入bfs就是至少存在一个中间点,即length = 2;
int length = 1;
while (!queue.isEmpty()) {
length++;
int size = queue.size();
for (int i = 0; i < size; i++) {
String word = queue.poll();
for (String nextWord : getNextWord(word, dict)) {
if (hash.contains(nextWord)) {
continue;
}
// bfs的出口
if (nextWord.equals(end)) {
return length;
}
hash.add(nextWord);
queue.offer(nextWord);
}
}
}
return 0;
}
/* 通过一个单词怎么快速找到通过改变一个字母得到词典中得另一个单词
* 粗暴的方法就是for循环,通过for循环词典中的每一个单词中的字母
* 和当前单词字母一一对比如果只有一个字母不一样说明就知道该单词
* 是下一个变化的候选单词
* 时间复杂度O(NL)
* 单词数N, 单词长度L,当词典中有很多单词时这种解法太慢
* 换种思路:
* 遍历字母表只有26个字母,除去当前字母还有25个,
* 得到当前单词每个字母看看下一个会变成什么,
* 得到所有的可能性和字典里单词对比,留下字典里存在的单词
*/
private ArrayList<String> getNextWord(String word, Set<String> dict) {
ArrayList<String> nextWords = new ArrayList<>();
// O(25)
for (char c = 'a'; c <= 'z'; c++ ) {
// 遍历单词的每个字母,O(L)
for (int i = 0; i < word.length(); i++) {
// 跳过相同的字母
if (c == word.charAt(i)) {
continue;
}
// O(L)
String nextWord = replace(word, c, i);
// 此处注意时间复杂度是O(L),不是O(1)
// 只有包含在字典中的nextWord才可以被当做下一个状态
if (dict.contains(nextWord)) {
nextWords.add(nextWord);
}
}
}
return nextWords;
}
private String replace(String s, char c, int index) {
//由于字符串的不可变性,字符串变成字符组,再从字符组变回去才可以改变字符串
char[] chars = s.toCharArray();
chars[index] = c;
return new String(chars);
}
}
- LeetCode
public class Solution {
public int ladderLength(String start, String end, List<String> wordList) {
Set<String> dict = new HashSet<>();
for (String word : wordList) {
dict.add(word);
}
if (start.equals(end)) {
return 1;
}
HashSet<String> hash = new HashSet<String>();
Queue<String> queue = new LinkedList<String>();
queue.offer(start);
hash.add(start);
int length = 1;
while (!queue.isEmpty()) {
length++;
int size = queue.size();
for (int i = 0; i < size; i++) {
String word = queue.poll();
for (String nextWord: getNextWords(word, dict)) {
if (hash.contains(nextWord)) {
continue;
}
if (nextWord.equals(end)) {
return length;
}
hash.add(nextWord);
queue.offer(nextWord);
}
}
}
return 0;
}
// replace character of a string at given index to a given character
// return a new string
private String replace(String s, int index, char c) {
char[] chars = s.toCharArray();
chars[index] = c;
return new String(chars);
}
// get connections with given word.
// for example, given word = 'hot', dict = {'hot', 'hit', 'hog'}
// it will return ['hit', 'hog']
private ArrayList<String> getNextWords(String word, Set<String> dict) {
ArrayList<String> nextWords = new ArrayList<String>();
for (char c = 'a'; c <= 'z'; c++) {
for (int i = 0; i < word.length(); i++) {
if (c == word.charAt(i)) {
continue;
}
String nextWord = replace(word, i, c);
if (dict.contains(nextWord)) {
nextWords.add(nextWord);
}
}
}
return nextWords;
}
}