[LeetCode] Word Ladder
2015-06-25 01:00
375 查看
Well, this problem has a nice BFS structure.
Let's see the example in the problem statement.
Since only one letter can be changed at a time, if we start from
The idea is simpy to begin from
To simplify the problem, we insert
The code is as follows.
The above code can still be speed up if we also begin from
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
endinto
dict. Once we meet
endduring 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
dictonce it is visited.
The code is as follows.
class Solution { public: int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) { wordDict.erase(beginWord); wordDict.insert(endWord); int ladder = 2, len = beginWord.length(); queue<string> nextWords; nextWords.push(beginWord); while (!nextWords.empty()) { int num = nextWords.size(); for (int i = 0; i < num; i++) { string word = nextWords.front(); nextWords.pop(); for (int p = 0; p < len; p++) { char letter = word[p]; for (int j = 0; j < 26; j++) { word[p] = 'a' + j; if (wordDict.find(word) != wordDict.end()) { if (word == endWord) return ladder; wordDict.erase(word); nextWords.push(word); } } word[p] = letter; } } ladder++; } return 0; } };
The above code can still be speed up if we also begin from
end. Once we meet the same word from
startand
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).
class Solution { public: int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) { wordDict.erase(beginWord); wordDict.erase(endWord); unordered_set<string> nextWords; unordered_set<string> prevWords; nextWords.insert(beginWord); prevWords.insert(endWord); int ladder = 2, len = beginWord.length(); while (!nextWords.empty() && !prevWords.empty()) { if (nextWords.size() > prevWords.size()) swap(nextWords, prevWords); unordered_set<string>::iterator itr = nextWords.begin(); unordered_set<string> temp; for (; itr != nextWords.end(); itr++) { string word = *itr; for (int p = 0; p < len; p++) { char letter = word[p]; for (int j = 0; j < 26; j++) { word[p] = 'a' + j; if (prevWords.find(word) != prevWords.end()) return ladder; if (wordDict.find(word) != wordDict.end()) { temp.insert(word); wordDict.erase(word); } } word[p] = letter; } } swap(nextWords, temp); ladder++; } return 0; } };
相关文章推荐
- iframe+dialog实现对话框为iframe
- LVS-Web基础构架:负载均衡和LVS
- LVS-【大型网站技术实践】初级篇:借助LVS+Keepalived实现负载均衡
- Android 存储数据SharedPreferences详解
- Launchpad图标出错,恢复Launchpad
- windows下wamp使用须知
- android--超级手电筒的开发记录
- VS2015 错误 C4996 'fopen': This function or variable may be unsafe.
- Linux操作系统配置maven
- 优化SqlServer--数据压缩
- Project Seasons Work In Progress 4
- linux下mysql的远程连接
- 特种设备
- log4j2 使用详解
- IOS开的property要点总结
- 解决Oracle ORA-12505 error
- 字符串操作汇总 C/C++
- 如何搞好团建和提高团队执行力?
- 野指针和内存泄露
- JTable 和 AbstractTableModel 几个关键点