【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 ...
随机推荐
- Open Yale course:Listening to Music
一.Introductionhttps://app.yinxiang.com /Home.action?offer=www_menu#n=4b034a29-986d-4914-8220-eb99c2e ...
- jQuery 取值、赋值的基本方法
转载:http://www.cnblogs.com/huanhuan86/archive/2012/06/13/2548071.html 获取元素的value值: /*获得TEXT.AREATEXT的 ...
- Mysql 常用函数
统计函数: count() 统计记录条数,如 select count(*) from stu; sum() 统计记录字段的和,如select sum(salary) from emp; ...
- Android Studio-设置放大代码编辑区
原有的快捷键是ctrl+shift+F12,现在我修改成了Ctrl+M.
- 思维导图-javascript(转)
学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将po出8张javascript相关的思维导图. 思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又 ...
- 初探Ajax
1.什么是Ajax Ajax是Asynchronous JavaScript and XML的缩写,这一技术能从服务器请求额外数据而无需卸载页面.传统的HTTP请求流程大概是这样的,浏览器向服务器发送 ...
- PHP基础封装简单的MysqliHelper类
MysqliHelper.class.php 1: <?php 2: 3: /** 4: * MysqliHelper 5: * [面向对象的MysqliHelper的简单封装] 6: */ ...
- POJ 1925 Spiderman
Spiderman Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5858 Accepted: 1143 Description ...
- 部分LINUX系统由图形界面启动变更为命令行界面启动的方法
背景: 图形界面很绚丽,但是现在并不需要图形界面,只需要命令行即可,所以要将图形界面自启动给关闭. 正文: Centos: 更改文件/etc/inittab的其中一行 id:5 ...
- 详解 iOS 上机题!附个人见解
庸者的救赎2015-11-20 02:30:23 AFN那个使用的时候不需要弱引用的,因为从你的封装方式来看,那个block并不会被当前视图控制器持有,而是被manager持有了,所以不需要__wea ...