Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

  1. Only one letter can be changed at a time
  2. 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.
 
 
 
先使用BFS,得到father,记录广度搜索路径上,每一个节点的父节点(可以有多个)
因此采用 unordered_map<string,unordered_set<string>> 数据结构
 
例如father["aaa"]=["baa","caa"]表示了aaa在广度优先搜素路径上的父节点为"baa","caa";
 
广度优先搜索时,从start开始,
找到与start相邻的节点 node1,node2,.....;
并记录每一个节点的父节点为start
 
然后从node1,node2...开始,遍历下一层
 
直到找到end节点停止(注意,必须上一层节点全部遍历完
 
 
找到father 路径后,从end开始往前dfs就可以得到所有的结果了
 
 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的更多相关文章

  1. 【leetcode】Word Ladder II(hard)★ 图 回头看

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  2. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  3. 【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 ...

  4. 【题解】【字符串】【BFS】【Leetcode】Word Ladder

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  5. 【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 ...

  6. 【leetcode】Word Ladder (hard) ★

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  7. 【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 ...

  8. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  9. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

随机推荐

  1. Open Yale course:Listening to Music

    一.Introductionhttps://app.yinxiang.com /Home.action?offer=www_menu#n=4b034a29-986d-4914-8220-eb99c2e ...

  2. jQuery 取值、赋值的基本方法

    转载:http://www.cnblogs.com/huanhuan86/archive/2012/06/13/2548071.html 获取元素的value值: /*获得TEXT.AREATEXT的 ...

  3. Mysql 常用函数

    统计函数: count()   统计记录条数,如 select count(*) from stu; sum()     统计记录字段的和,如select sum(salary) from emp; ...

  4. Android Studio-设置放大代码编辑区

    原有的快捷键是ctrl+shift+F12,现在我修改成了Ctrl+M.

  5. 思维导图-javascript(转)

    学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将po出8张javascript相关的思维导图. 思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又 ...

  6. 初探Ajax

    1.什么是Ajax Ajax是Asynchronous JavaScript and XML的缩写,这一技术能从服务器请求额外数据而无需卸载页面.传统的HTTP请求流程大概是这样的,浏览器向服务器发送 ...

  7. PHP基础封装简单的MysqliHelper类

    MysqliHelper.class.php 1: <?php 2:  3: /** 4: * MysqliHelper 5: * [面向对象的MysqliHelper的简单封装] 6: */ ...

  8. POJ 1925 Spiderman

    Spiderman Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5858 Accepted: 1143 Description ...

  9. 部分LINUX系统由图形界面启动变更为命令行界面启动的方法

    背景: 图形界面很绚丽,但是现在并不需要图形界面,只需要命令行即可,所以要将图形界面自启动给关闭.    正文:   Centos:     更改文件/etc/inittab的其中一行    id:5 ...

  10. 详解 iOS 上机题!附个人见解

    庸者的救赎2015-11-20 02:30:23 AFN那个使用的时候不需要弱引用的,因为从你的封装方式来看,那个block并不会被当前视图控制器持有,而是被manager持有了,所以不需要__wea ...