【leetcode】Word Ladder II
Word Ladder II
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.
class Solution {
public:
vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) { vector<vector<string>> result;
unordered_set<string> unvisited=dict; dict.insert(start);
dict.insert(end); unordered_map<string,unordered_set<string>> father;
if(unvisited.count(start)==) unvisited.erase(start); unordered_set<string> curString,nextString;
curString.insert(start); while(curString.count(end)==&&curString.size()>)
{ for(auto it=curString.begin();it!=curString.end();it++)
{
string word=*it;
for(int i=;i<word.length();i++)
{
string tmp=word;
for(int j='a';j<='z';j++)
{
if(tmp[i]==j) continue;
tmp[i]=j;
if(unvisited.count(tmp)>)
{
nextString.insert(tmp);
father[tmp].insert(word); } }
}
} if(nextString.size()==) break; for(auto it=nextString.begin();it!=nextString.end();it++)
{
//必须遍历完了curString中所有的元素,才能在unvisited中删除(因为可能有多个父节点对应着该节点)
unvisited.erase(*it);
} curString=nextString;
nextString.clear(); } if(curString.count(end)>)
{
vector<string> tmp;
dfs(father,end,start,result,tmp);
} return result;
} void dfs(unordered_map<string,unordered_set<string>> &father,string end,string start,vector<vector<string>> &result,vector<string> tmp)
{
tmp.push_back(end);
if(end==start)
{
reverse(tmp.begin(),tmp.end());
result.push_back(tmp);
return;
} for(auto it=father[end].begin();it!=father[end].end();it++)
{
dfs(father,*it,start,result,tmp);
}
}
};
【leetcode】Word Ladder II的更多相关文章
- 【leetcode】Word Ladder II(hard)★ 图 回头看
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- 【leetcode】Word Ladder
Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...
- 【leetcode】Word Break II
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- 【leetcode】Word Search II(hard)★
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 【leetcode】Word Ladder (hard) ★
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- 【leetcode】Word Break II (hard)★
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [Leetcode Week5]Word Ladder II
Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...
随机推荐
- Mysql 排名查询
原文地址: http://www.cnblogs.com/songshuai/p/5688550.html http://blog.csdn.net/u010503822/article/detail ...
- sql语句积累
有一个需求表(demand),每一记录就是一条需求:另外有一个报价表(quotation),每一条记录是对需求记录的报价详情. 需求表: 报价表: 我现在想得到每条需求的信息以及有多少人报价了,我们可 ...
- 源码编译安装 screen
本文转自:http://blog.163.com/oracle_wwf/blog/static/213030127201211191481101/ [root@web1 soft]# wget ftp ...
- Eclipse闪退无法打开的解决方法
使用Eclipse过程中但是有时会出现打不开闪退的情况,这是为什么呢,遇到这种情况怎么解决.东坡小编通过查找资料,发现如下方法可以解决eclipse打不开闪退,具体操作如下: Eclipse打不开闪退 ...
- Ruby注意事项
在Ruby中只有false和nil是'假', 其余都是真(0也是真)
- solrcloud
@Test public void querySolrCloud(){ String zkHost = "127.0.0.1:2181"; String defaultCollec ...
- 1.交通聚类:编辑距离 (Levenshtein距离)Java实现
1.最近工作中要实现用户车辆的行驶路线的聚类,由于所给的数据只有用户一天中交通卡口所监视的卡口名称 :即青岛路-威海路-济阳路 . 要通过聚类实现车辆路线的规律分析,首先要解决的是相似度问题,我们知道 ...
- plink远程连接服务器进行编译
脚本命令: echo y|D:\remote_link\plink -l user -pw password 172.16.0.101 "export LANG=en_US;cd / ...
- C#高级知识点01---委托和事件
委托和事件 什么是委托? 简单来说,就是能把方法当作参数传递的对象,而且还知道怎么去调用这个方法,同时还约束了方法的签名. 例子: 用委托实现插件式编程: 1.
- javascript中对象学习
第一篇文章: javascript中this关键字的详细解析: http://blog.csdn.net/wyj880220/article/details/7305952 Javascript ...