描述
給出兩個單詞(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;
}
}