LeetCode----Word Ladder 2
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) 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"]
Return
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
Note:
- All words have the same length.
- All words contain only lowercase alphabetic characters.
分析:
给定一个单词start, 问通过一系列变换是否能够得到单词end, 中间的单词都必须是给定的字典中的词。
其实就是 从最初的一个状态,能够找到一个path, 到达最终的一个状态。
Bingo! 图的搜索: 找到图中两个node之间的所有的最短路径。
图的搜索有 深度优先(DFS) 和 广度优先(BFS)两种。
BFS适用于解决寻找最短路径的问题,因此采用BFS
class Solution {
public:
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
//BFS each node in graph has multipul fathers
vector<vector<string> > result;
vector<string> path;
bool found = false;
unordered_set<string> visited;
unordered_map<string, vector<string> > father;
unordered_set<string> current, next;
if(start.size() != end.size()) return result;
current.insert(start);
while(!current.empty())
{
for(auto i : current)
visited.insert(i);
for(auto str : current)
{
for(size_t i=0; i<str.size(); ++i)
{
string new_str(str);
for(char c = 'a'; c <= 'z'; ++c)
{
if(c == new_str[i]) continue;
swap(c, new_str[i]);
if(new_str == end || dict.count(new_str) > 0 && !visited.count(new_str)){
//visited.insert(new_str);
next.insert(new_str);
father[new_str].push_back(str);
}
if(new_str == end){
found = true;
break;
}
swap(c, new_str[i]);
}
}
}// end for
current.clear();
if(!found){
swap(current, next);
}
}// end while
buildPath(father, path, start, end, result);
return result;
}
private:
void buildPath(unordered_map<string, vector<string> > &father, vector<string> &path,
const string &start, const string &word, vector<vector<string> > &result){
path.push_back(word);
if(word == start){
result.push_back(path);
reverse(result.back().begin(), result.back().end());
}
else{
for(auto f : father[word])
buildPath(father, path, start, f, result);
}
path.pop_back();
}
};
LeetCode----Word Ladder 2的更多相关文章
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- [leetcode]Word Ladder @ Python
原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...
- Leetcode Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
随机推荐
- js中tagName和nodeName
DOM里常见的三种节点类型(总共有12种,如docment):元素节点,属性节点以及文本节点,例如<h2 class="title">head</h2>,其 ...
- iOS 10 消息推送(UserNotifications)秘籍总结(二)
背景 上一篇博客iOS 10 消息推送(UserNotifications)秘籍总结(一)发布后被 简书编辑推荐至首页,这着实让我受宠若惊啊.可是好事不长,后面发生了让我伤心欲绝的事,我的女朋友不要我 ...
- android.support.v4.app.Fragment和android.app.Fragment区别
1.最低支持版本不同 android.app.Fragment 兼容的最低版本是android:minSdkVersion="11" 即3.0版 android.support.v ...
- Excel 函数记录
1.四舍五入:round(数据,小数位数)
- linux 一些命令
1.查看cpu个数 cat /proc/cpuinfo |grep "physical id" |sort | uniq |wc -l 2 2.查看cpu逻辑个数 cat /pro ...
- java成员变量与局部变量修饰符的区别
成员变量: 可以被 public,static ,protected,default,final修饰. 局部变量:包括方法里的和 代码块里的(静态和非静态) 可以被default, final修饰 参 ...
- 主机无法访问虚拟机上的elasticsearch服务器
问题: es在linux上搭建好,通过curl -XGET ip:port可以获得服务器信息展示,但是主机在浏览器上无法访问. 原因: 主机通过telnet访问linux的80端口,发现是不通的.可以 ...
- ARM安装ROS- indigo
Ubuntu ARM install of ROS Indigo 溪西创客小屋 There are currently builds of ROS for Ubuntu Trusty armhf. T ...
- Windows7下U盘安装Ubuntu14.04双系统
1.准备工作 (1)下载Ubuntu14.04系统镜像文件,Ultraiso,EasyBcd,分区助手 Ubuntu14.04地址:http://www.ubuntu.com/download/des ...
- C#根据当前日期获取星期和阴历日期
private string GetWeek(int dayOfWeek) { string returnWeek = ""; switch (dayOfWeek) { case ...