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. jquery学习笔记1

    (1) jQuery的Id选择器: $("#btnShow") (2) 事件绑定函数 bind() $("#btnAdd").bind("click& ...

  2. cookie、sessionStorage、localStorage区别

    相同:不管sessionStorage localStorage 还是 cookie 都是存储用户数据的. 不同: 1.cookie的存储空间小, cookie的数据是会通过http请求带到服务器的( ...

  3. 自动发牌(C#版)

    利用数组实现发牌过程 一副牌去掉大小王,还剩52张.一共东.南.西.北四家,每家随机发13张牌. 提示: 东.南.西.北四家用一维数组表示 每家的牌采用一维数组表示(13张)  花色:enum Sui ...

  4. Android res/目录下子目录详解

    Directory Resource Type animator/ XML files that define property animations. anim/ XML files that de ...

  5. Response返回JSON数据到前台页面

    转自博文:<Response JSON数据返回>http://blog.csdn.net/anialy/article/details/8665471 简述: 在servlet填充Resp ...

  6. Javascript之高效编程

    前言: Javascript绝对是最火的编程语言之一,一直具有很大的用户群,具有广泛的应用前景.而在前端开发中,它也是三驾马车之一,并且是最重要的一环.要想给用户提供更流畅的操作体验,更友好的交互,对 ...

  7. 引用CSS文件到html网页里方法

        引用CSS文件到Html方法-css引入,css引用 使用不同的方法来引用css样式表,最终到达的效果相同,但是使用不同方法应用的css文件将影响到SEO及网页打开速度效率. html引用cs ...

  8. freemarker实例2

    下面演示一个简单的使用项目过程:1. 使用myeclipse创建一个web项目testFM2. 把下载到的jar包(freemarker-2.3.9.jar)放到/WebRoot/WEB-INF/li ...

  9. js里面引入js

    document.write('<script src="http://js.xcar.com.cn/bbs/sidebar/js/publicSidebar.js"> ...

  10. Windows多线程编程总结

    1 内核对象 1 .1 内核对象的概念 内核对象是内核分配的一个内存块,这种内存块是一个数据结构,表示内核对象的各种特征.并且只能由内核来访问.应用程序若需要访问内核对象,需要通过操作系统提供的函数来 ...