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一些题目
假期在家,看到的,昨天看了下: 原文链接:http://wwwcqamin.duapp.com/?p=102#comment-7 请说出下面程序的输出结果 第一题: 1 2 3 4 5 6 7 8 9 ...
- 《javascript高级程序设计》第七章 递归recursion
7.1 递归7.2 闭包 7.2.1 闭包与变量 7.2.2 关于this 对象 7.2.3 内存泄漏 7.3 模仿块级作用域7.4 私有变量 7.4.1 静态私有变量 7.4.2 模块模式 7.4. ...
- hdu----(3068)最长回文(manacher)
最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 初学java之触发响应事件
import java.awt.*; import javax.swing.*; import java.awt.event.*; class WindowActionEvent extends JF ...
- codeforces723 D. Lakes in Berland(并查集)
题目链接:codeforces723 D. Lakes in Berland 参考博客:http://www.cnblogs.com/Geek-xiyang/p/5930245.html #inclu ...
- cms3.0——收获(1)
或许是由于各个公司的情况不同,使得每次写后台管理系统就沿用之前的nodejs中的thinkjs来写后台管理系统,也是因为这样后期维护起来更加方便吧?不过最早之前的项目,却有一个使用的是nodejs 中 ...
- BZOJ一天提交 51纪念(二)
今天作死又交了一发呢...于是屯题就全用完啦~ 有一次拷错CE,还有一次本来的程序就是错的的说... 可是我希望看到我努力的人并不会看到我的努力呢,尽管如此一个人也要坚持走到底哦,就如同这不完美的提交 ...
- Java如何将控制台上的结果保存到文件
无论是二进制数据还是字符数据(文本数据),都可以用文件输出流java.io.FileOutputStream,以字节流的方式保存到指定文件. package test; import java.io. ...
- bzoj 2561: 最小生成树
#include<cstdio> #include<iostream> #include<cstring> #define M 100009 #define inf ...
- 使用ASP.Net WebAPI构建REST服务(六)——Self-Host
Asp.Net WebAPI生成的是一个程序集,并不是独立的进程,因此,要运行的时候必须将其承载在相应的宿主上,一般比较常见的是IIS承载.很多时候,我们为了简化部署或者功能集成,需要将其承载到独立的 ...