LeetCode之Word Ladder
来源:互联网 发布:网络流行语 英文 编辑:程序博客网 时间:2024/04/28 03:11
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog"
,
return its length 5
.
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
Program Runtime: 12 milli secs
class Solution {
private:
int difchar(string s, string e)
{
int len = s.length();
int num = 0;
for(int i = 0; i < len; i++)
{
if(s[i]!=e[i])
num++;
}
return num;
}
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<string> search;
if(difchar(start, end) == 0)
{
return 1;
}
if(difchar(start, end) == 1)
{
return 2;
}
unordered_set<string>::iterator it = dict.begin();
while(it!=dict.end())
{
if((*it) == start || (*it) == end)
it = dict.erase(it);
else
it++;
}
if(dict.empty())
return 0;
it = dict.begin();
while(it!=dict.end())
{
if(difchar(start, *it) == 1)
{
search.push_back(*it);
it = dict.erase(it);
}
else
it++;
}
if(search.empty())
return 0;
vector<string>::iterator ip = search.begin();
int min = 0;
while(ip!=search.end())
{
int n = ladderLength(*ip, end, dict);
if(n == 0)
{
ip++;
continue;
}
if(min == 0)
min = n+1;
else if(min>n+1)
min = n+1;
ip++;
}
return min;
}
};
测试用例:
input output expected "a", "c", ["a","b","c"]22 "hot", "dog", ["hot","dog"]00"hot", "dog", ["hot","dog","dot"]33"hot", "dot", ["hot","dot","dog"]22"hot", "dog", ["hot","cog","dog","tot","hog","hop","pot","dot"]33"hot", "dog", ["hot","dog","cog","pot","dot"]33"hit", "cog", ["hot","cog","dot","dog","hit","lot","log"]55"hit", "cog", ["hot","hit","cog","dot","dog"]55"red", "tax", ["ted","tex","red","tax","tad","den","rex","pee"]44"lost", "cost", ["most","fist","lost","cost","fish"]22"lost", "miss", ["most","mist","miss","lost","fist","fish"]44"leet", "code", ["lest","leet","lose","code","lode","robe","lost"]66"talk", "tail", ["talk","tons","fall","tail","gale","hall","negs"]00
- LeetCode之Word Ladder
- leetcode之word ladder
- LeetCode 之 Word Ladder
- leetcode 之 Word Ladder
- leetcode 之 Word Ladder
- LeetCode之Word Ladder
- LeetCode之Word Ladder
- Leetcode之word-ladder
- LeetCode之Word Ladder II
- LeetCode OJ 之 Word Ladder (字梯)
- [Leetcode][python]Word Ladder/Word Ladder II
- [Leetcode] Word Ladder
- LeetCode: Word Ladder
- leetcode 84: Word Ladder
- [leetcode] word ladder
- 【leetcode】Word Ladder
- 【leetcode】Word Ladder II
- LeetCode - Word Ladder
- XBMC视频插件开发系列--网页数据解析
- HDU 4342 ——History repeat itself
- linux系统编程之进程(四):wait/waitpid函数与僵尸进程、fork 2 times
- <python>random模块
- 使用命令行登陆Mysql
- LeetCode之Word Ladder
- js 中 setTimeout()的使用
- Web高性能开发之前端高性能(一)
- FPGA同步复位,异步复位以及异步复位同步释放实例分析
- hadoop的使用记录
- hdu 2089 不要62 数位dp
- hibernate 二级缓存
- XBMC如何删除一个插件
- priority_queue的用法