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. Python Paramiko模块与MySQL数据库操作

    Paramiko模块批量管理:通过调用ssh协议进行远程机器的批量命令执行. 要使用paramiko模块那就必须先安装这个第三方模块,仅需要在本地上安装相应的软件(python以及PyCrypto), ...

  2. visio2007使用记录

    安装时, 需要有选择性的安装, 不是所有的组件 都需要安装, 如office工具, office共享功能, visio中的.net可编程支持就用不着安装.... visio中的cad(加载项), 可以 ...

  3. thinkphp-许愿墙-3

    用jquery写异步传递的时候, 首先要判断表单中的输入是否为空: 如果有多个输入项, 应该, 分别的, 一步一步的来判断是否为空, 而不是用 and / or来复合判断! 同时如果为空, 应该将它设 ...

  4. thinkphp-许愿墙-2

    在数组中,也可以使用函数,如: $data = array( 'username'=> I('username','', 'htmlspecailchars'), 'content'=> ...

  5. Python-时间操作

    目录 Python标准模块 datetime 数据类型 datetime timedelta 字符串和datetime的相互转换 datetime 转 字符串 str strftime strftim ...

  6. springMVC 缓存(入门 spring+mybaties+redis一)

    使用redis之前需要咋电脑上安装redis: 使用spring+mybaties+redis的本质是扩展类   org.apache.ibatis.cache.Cache:在我们自己扩展的Cache ...

  7. linxu scp命令

    \ svn 删除所有的 .svn文件 find . -name .svn -type d -exec rm -fr {} \; linux之cp/scp命令+scp命令详解   名称:cp 使用权限: ...

  8. Springmvc + mybatis + spring 配置,spring事物

    今天配置了半天,发现,事物不起效果,主要出现如下错误: org.mybatis.spring.transaction.SpringManagedTransaction] - [JDBC Connect ...

  9. 给一系列的div中的第一个添加class

     $(".lb:first").addClass("active")

  10. PHP命名空间入门教程

    PHP5.3当中就引入了“命名空间”的概念,一直都没怎么关注和使用,其实学习它也挺简单的,看官方的教程就行了: 命名空间概述 定义命名空间 定义子命名空间 在同一个文件中定义多个命名空间 使用命名空间 ...