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打印图形
1. js绘画金字塔 思想:先画n-i个空格,再画2*i-1个*号,再画n-i个空格(此处可以省略),一行画完之后换行:循环下一行(先判断每行的空格数和*号与行数间的关系) var n=window ...
- addViewController之后view里面的点击事件不响应
let dealsSeeMoreViewController = DealsSeeMoreViewController(owner: self) self.dealsStackView.addArra ...
- Java 多线程——基础知识
java 多线程 目录: Java 多线程——基础知识 Java 多线程 —— synchronized关键字 java 多线程——一个定时调度的例子 java 多线程——quartz 定时调度的例子 ...
- 多分类问题中,实现不同分类区域颜色填充的MATLAB代码(demo:Random Forest)
之前建立了一个SVM-based Ordinal regression模型,一种特殊的多分类模型,就想通过可视化的方式展示模型分类的效果,对各个分类区域用不同颜色表示.可是,也看了很多代码,但基本都是 ...
- Linux 远程桌面控制
我现在知道有两种方式: 1.直接使用Gnome桌面的远程控制功能.在服务器端登录到gnome桌面,然后在系统菜单中打开远程桌面配置,勾选允许远程即可.这种方式客户端和服务器的两种操作将保持同步,也就是 ...
- 谈谈CSS的布局,display、position、float
前言 前端一直是我的一个很大的缺憾,这段时间痛顶思痛,决定好好的把前台的东西加强,这不,在学习了一段时间js之后,在做一些小练习,却发现最基本的一些css知识却还不了解,所以便有了这篇博文. 块级元素 ...
- centos 运用ssh的rsa算法实现无密码登录
ssh 公钥和私钥原理 1.客户端机子生成私钥和公钥,将公钥放到服务器证书中,然后就可以实现免密码登录.(服务器认证文件要有该登录用户的读执行权限) 2.a登录b: a机子:test01账号(b也要建 ...
- Mongodb Management Studio
1.服务器管理功能添加服务器,删除服务器 2.服务器,数据库,表,列,索引,树形显示和状态信息查看 3.查询分析器功能.支持select,insert,Delete,update支持自定义分页函数 $ ...
- bzoj 2428: [HAOI2006]均分数据
#include<cstdio> #include<iostream> #include<cstdlib> #include<ctime> #inclu ...
- IT公司100题-5-查找最小的k个元素
问题描述: 输入n 个整数,输出其中最小的k 个. 例如输入8, 7, 6, 5, 4, 3, 2, 1这8 个数字,则最小的3 个数字为3, 2, 1. 分析: 时间复杂度O(nlogn)方法: ...