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.

分析:

给定一个单词start, 问通过一系列变换是否能够得到单词end, 中间的单词都必须是给定的字典中的词。

其实就是 从最初的一个状态,能够找到一个path, 到达最终的一个状态。

Bingo! 图的搜索: 找到图中两个node之间的所有的最短路径。

图的搜索有 深度优先(DFS) 和 广度优先(BFS)两种。

BFS适用于解决寻找最短路径的问题,因此采用BFS

class Solution {
public:
vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
//BFS each node in graph has multipul fathers
vector<vector<string> > result;
vector<string> path;
bool found = false;
unordered_set<string> visited;
unordered_map<string, vector<string> > father;
unordered_set<string> current, next; if(start.size() != end.size()) return result; current.insert(start);
while(!current.empty())
{
for(auto i : current)
visited.insert(i); for(auto str : current)
{
for(size_t i=0; i<str.size(); ++i)
{
string new_str(str);
for(char c = 'a'; c <= 'z'; ++c)
{
if(c == new_str[i]) continue; swap(c, new_str[i]);
if(new_str == end || dict.count(new_str) > 0 && !visited.count(new_str)){
//visited.insert(new_str);
next.insert(new_str);
father[new_str].push_back(str);
}
if(new_str == end){
found = true;
break;
}
swap(c, new_str[i]);
}
}
}// end for
current.clear();
if(!found){
swap(current, next);
}
}// end while buildPath(father, path, start, end, result);
return result;
}
private:
void buildPath(unordered_map<string, vector<string> > &father, vector<string> &path,
const string &start, const string &word, vector<vector<string> > &result){
path.push_back(word);
if(word == start){
result.push_back(path);
reverse(result.back().begin(), result.back().end());
}
else{
for(auto f : father[word])
buildPath(father, path, start, f, result);
}
path.pop_back();
}
};

LeetCode----Word Ladder 2的更多相关文章

  1. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  2. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  3. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  4. [LeetCode] Word Ladder II 词语阶梯之二

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

  5. LeetCode :Word Ladder II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

  6. LeetCode: Word Ladder II 解题报告

    Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...

  7. LeetCode: Word Ladder II [127]

    [题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  8. LeetCode Word Ladder 找单词变换梯

    题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...

  9. [leetcode]Word Ladder @ Python

    原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...

  10. Leetcode Word Ladder

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

随机推荐

  1. Tomcat Manager用户配置详解

      Tomcat Manager是Tomcat自带的.用于对Tomcat自身以及部署在Tomcat上的应用进行管理的web应用.Tomcat是Java领域使用最广泛的服务器之一,因此Tomcat Ma ...

  2. python中文处理

    源码文件为utf-8格式  CODEC = 'utf-8': VS在“高级保存选项”中选择“UTF-8 65001” input(u'中文');print(u'中文')

  3. php不解析的排查步骤

    php不解析的排查步骤:1. /usr/local/apache2/bin/apachectl -M 看一下有没有加载libphp5.so2. 查看配置文件中是否有 AddType applicati ...

  4. Jquery.Page.js 分页插件的使用

    1.简单直接贴代码 需要引用以下样式和脚本 <link href="~/Scripts/Page/pager.css" rel="stylesheet" ...

  5. CSS 去除列表项li前面的小圆点

    前言 在默认的情况下,浏览器会在<li>标签前面加上小圆点形成列表项,如下所示使用<ul> 项1 项2 项3 或者使用<ol> 项1 项2 项3 这在有时候确实给我 ...

  6. cstring 的重载

    #include <iostream> #include <windows.h> using namespace std; +; class MyString { public ...

  7. 下拉刷新列表添加SwipeDismissListViewTouchListener实现滑动删除某一列。

    <Android SwipeToDismiss:左右滑动删除ListView条目Item> Android的SwipeToDismiss是github上一个第三方开源框架(github上的 ...

  8. PowerMock遇到的问题——4

    当我们在测试一个方法的构造方法的时候,有的时候内部需要new一些对象,这是就需要用到PowerMock.exceptNew(),这个方法,但有时候传的参数有关键字this,比如SAPPublisher ...

  9. C#生成唯一的ID保存到数据库

    直接用.NET Framework 提供的 Guid() 函数: Guid.NewGuid()是指生成唯一码的规则 System.Guid.NewGuid().ToString()全球唯一标识符 (G ...

  10. CRF++使用小结

    1. 简述 最近要应用CRF模型,进行序列识别.选用了CRF++工具包,具体来说是在VS2008的C#环境下,使用CRF++的windows版本.本文总结一下了解到的和CRF++工具包相关的信息. 参 ...