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. setObject()用法

    setObject()用法 setObject(i+1,arg[i])用法与setInt(i+1,arg[i]),setString(i+1,arg[i])用法类似, 但不需指定参数类型,可以通用: ...

  2. Page传回页面的值问题

  3. 使用微信JS-SDK调用微信浏览器的接口

    先附上微信公众平台的相关链接: 微信公众平台:https://mp.weixin.qq.com/ 微信公众平台开发文档:https://mp.weixin.qq.com/wiki 微信公众平台JS-S ...

  4. [c#]params可变参数

    摘要 在项目中多多少少会用到params这个关键字,来修饰参数,它的作用,让该参数的个数是可变的,并且可变参数必须是方法的最后一个参数.但如何判断到底有没有为该参数传递值,怎么判断? 一个例子 sta ...

  5. [MongoDB]可视化工具Robomongo

    摘要 习惯了可视化的管理数据的方式,通过敲命令,确实有些不自在.这里推荐一个mongodb的可视化工具——Robomongo 相关文章 [MongoDB]入门操作 [MongoDB]增删改查 [Mon ...

  6. smarty string_format用法 取小数点后2位

    <{if $d.ul_pv}> <{$d.sum/$d.ul_pv|string_format:'%.2f'}> <{else}> 0.00 <{/if}&g ...

  7. Linux服务器管理: 日志管理(一)

    1.日志管理介绍: a.日志服务:在CentOS6.x中日志服务以及由rsyslogd取代了原有的syslogd服务.rsyslogd日志服务更加先进,功能更多.但是不论该服务的使用,还是日子文件的格 ...

  8. ansible的使用技巧

    #查看ansible的帮助 $ ansible -h   #ansible 指定不通的模块执行 $ ansible -i /etc/ansible/hosts  docker -u root -m c ...

  9. 用css布局的方法实现如果字符超过一定长度就显示成省略号

    以前实现这种效果需要在程序里判断字符的长度,如果长度大于多少个字符,就截取字符,用省略号代替,而且是在服务器处理的,现在只需要用css的属性来操作,简单.实用.节省性能.不用做过多的程序判断.节约开发 ...

  10. 在Linux上配置无线网络

    导读 iwconfig是Linux Wireless Extensions(LWE)的用户层配置工具之一.LWE是Linux下对无线网络配置的工具,包括内核的支持.用户层配置工具和驱动接口的支持三部分 ...