[LeetCode] Word Ladder

简介: Well, this problem has a nice BFS structure. Let's see the example in the problem statement. start = "hit" end = "cog" dict = ["hot", "dot", "dog"...

Well, this problem has a nice BFS structure.

Let's see the example in the problem statement.

start = "hit"

end = "cog"

dict = ["hot", "dot", "dog", "lot", "log"]

Since only one letter can be changed at a time, if we start from "hit", we can only change to those words which have only one different letter from it, like "hot". Putting in graph-theoretic terms, we can say that "hot" is a neighbor of "hit".

The idea is simpy to begin from start, then visit its neighbors, then the non-visited neighbors of its neighbors... Well, this is just the typical BFS structure.

To simplify the problem, we insert end into dict. Once we meet end during the BFS, we know we have found the answer. We maintain a variable ladder for the current distance of the transformation and update it by ladder++ once we finish a round of BFS search (note that it should fit the definition of the distance in the problem statement). Also, to avoid visiting a word for more than once, we erase it from dict once it is visited.

The code is as follows.

 1 class Solution {
 2 public:
 3     int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
 4         wordDict.erase(beginWord);
 5         wordDict.insert(endWord);
 6         int ladder = 2, len = beginWord.length();
 7         queue<string> nextWords;
 8         nextWords.push(beginWord);
 9         while (!nextWords.empty()) {
10             int num = nextWords.size();
11             for (int i = 0; i < num; i++) {
12                 string word = nextWords.front();
13                 nextWords.pop();
14                 for (int p = 0; p < len; p++) {
15                     char letter = word[p];
16                     for (int j = 0; j < 26; j++) {
17                         word[p] = 'a' + j;
18                         if (wordDict.find(word) != wordDict.end()) {
19                             if (word == endWord) return ladder;
20                             wordDict.erase(word);
21                             nextWords.push(word);
22                         }
23                     }
24                     word[p] = letter;
25                 }
26             }
27             ladder++;
28         }
29         return 0;
30     }
31 };

The above code can still be speed up if we also begin from end. Once we meet the same word from start and end, we know we are done. This link provides a nice two-end search solution. I rewrite the code below for better readability. At each round of BFS, depending on the relative size of nextWords and prevWords, we swap the smaller one to the working setto reduce the running time (lines 12 - 13). 

 1 class Solution {
 2 public:
 3     int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) {
 4         wordDict.erase(beginWord);
 5         wordDict.erase(endWord);
 6         unordered_set<string> nextWords;
 7         unordered_set<string> prevWords;
 8         nextWords.insert(beginWord);
 9         prevWords.insert(endWord);
10         int ladder = 2, len = beginWord.length();
11         while (!nextWords.empty() && !prevWords.empty()) {
12             if (nextWords.size() > prevWords.size())
13                 swap(nextWords, prevWords);
14             unordered_set<string>::iterator itr = nextWords.begin();
15             unordered_set<string> temp;
16             for (; itr != nextWords.end(); itr++) {
17                 string word = *itr;
18                 for (int p = 0; p < len; p++) {
19                     char letter = word[p];
20                     for (int j = 0; j < 26; j++) {
21                         word[p] = 'a' + j;
22                         if (prevWords.find(word) != prevWords.end())
23                             return ladder;
24                         if (wordDict.find(word) != wordDict.end()) {
25                             temp.insert(word);
26                             wordDict.erase(word);
27                         }
28                     }
29                     word[p] = letter;
30                 }
31             }
32             swap(nextWords, temp);
33             ladder++;
34         }
35         return 0;
36     }
37 };

 

目录
相关文章
LeetCode 318. Maximum Product of Word Lengths
给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
77 0
LeetCode 318. Maximum Product of Word Lengths
LeetCode 290. Word Pattern
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。 这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应模式。
49 0
LeetCode 290. Word Pattern
LeetCode 212. Word Search II
给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
79 0
LeetCode 212. Word Search II
|
索引
LeetCode 79. Word Search
给定一个2D板和一个单词,找出该单词是否存在于网格中。 该单词可以由顺序相邻的单元的字母构成,其中“相邻”单元是水平或垂直相邻的单元。 相同的字母单元格不得多次使用。
76 0
LeetCode 79. Word Search
|
索引 存储
LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50611509 翻译 给定一个模式,和一个字符串str,返回str是否符合相同的模式。
957 0
LeetCode 58 Length of Last Word(最后单词的长度)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50592917 翻译 给定一个包含大小写字母和空格字符的字符串, 返回该字符串中最后一个单词的长度。
728 0