126. Word Ladder II

题目

 Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

    Only one letter can be changed at a time
Each transformed word must exist in the word list. Note that beginWord is not a transformed word. For example, Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log","cog"] Return [
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
] Note: Return an empty list if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
You may assume no duplicates in the word list.
You may assume beginWord and endWord are non-empty and are not the same. UPDATE (2017/1/20):
The wordList parameter had been changed to a list of strings (instead of a set of strings). Please reload the code definition to get the latest changes.

解析

For the most voted solution, it is very complicated.
I do a BFS for each path
for example:
{hit} ->
{hit,hot} ->
{hit,hot,dot}/{hit,hot,lot} ->
[“hit”,“hot”,“dot”,“dog”]/[“hit”,“hot”,“lot”,“log”] ->
[“hit”,“hot”,“dot”,“dog”,“cog”],
[“hit”,“hot”,“lot”,“log”,“cog”]
class Solution_126 {
public:
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) { vector<vector<string>> res;
unordered_set<string> visit; //notice we need to clear visited word in list after finish this level of BFS
queue<vector<string>> q;
unordered_set<string> wordlist(wordList.begin(), wordList.end()); q.push({ beginWord });
bool flag = false; //to see if we find shortest path while (!q.empty()){
int size = q.size(); for (int i = 0; i < size; i++){ //for this level
vector<string> cur = q.front();
q.pop();
vector<string> newadd = addWord(cur.back(), wordlist);
for (int j = 0; j < newadd.size(); j++){ //add a word into path
vector<string> newline(cur.begin(), cur.end());
newline.push_back(newadd[j]); if (newadd[j] == endWord){
flag = true;
res.push_back(newline);
}
visit.insert(newadd[j]); // insert newadd word
q.push(newline);
}
} if (flag)
break; //do not BFS further for (auto it = visit.begin(); it != visit.end(); it++)
wordlist.erase(*it); //erase visited one
visit.clear();
} sort(res.begin(),res.end());
return res;
} // find words with one char different in dict
// hot->[dot,lot]
vector<string> addWord(string word, unordered_set<string>& wordlist){
vector<string> res;
for (int i = 0; i < word.size(); i++){
char s = word[i];
for (char c = 'a'; c <= 'z'; c++){
word[i] = c;
if (wordlist.count(word)) res.push_back(word);
}
word[i] = s;
}
return res;
}
};

题目来源

126. Word Ladder II(hard)的更多相关文章

  1. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

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

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  3. 126. Word Ladder II

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

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

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

  5. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  6. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  7. leetcode 126. Word Ladder II ----- java

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  8. Leetcode#126 Word Ladder II

    原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...

  9. 【leetcode】126. Word Ladder II

    题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从star ...

随机推荐

  1. jeakins+maven+jmeter构建性能测试自动化( 在eclipse里运行如果出现没有找到“*.loadtest.xls”,请将此文件名修改为你对应使用的xsl文件名)

    背景: 首先用jmeter录制或者书写性能测试的脚本,用maven添加相关依赖,把性能测试的代码提交到github,在jenkins配置git下载性能测试的代码,配置运行脚本和测试报告,配置运行失败自 ...

  2. htmilunit-- 针对抓取js生成的数据

    public static String  getHtml(String html){        // 模拟一个浏览器          @SuppressWarnings("resou ...

  3. mysql再次安装问题

    安装过一次mysql的电脑,想再安装或更换其它版本的mysql.在重新安装的最后一步,总会出现这样的问题. 网上说法也很多,什么删除注册表了等等.这都是狗屁. 真正的做法是找到C盘下的隐藏文件夹Pro ...

  4. script error总结

    移动端的页面在控制台报出一个script error,通常的原因有一下几点: 1. 脚本引入错误 可能是脚本的地址不对,协议不对(http或https问题),本地host文件绑定的地址不对 2. 方法 ...

  5. 关于ContextImp的源码分析

      关于ContextImp的源码分析 来源: http://blog.csdn.net/qinjuning/article/details/7310620 Context概述:   Android ...

  6. error: ‘to_string’ was not declared in this scope

    错误: error: ‘to_string’ was not declared in this scope 原因: to_string是C++11引入的新功能,旧版本编译器可能不支持它,所以要给编译器 ...

  7. Python matplotlib 柱状图

    matplotlib是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中.它的文档相当完备,并且 ...

  8. [暑假集训--数论]poj3518 Prime Gap

    The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...

  9. 基于webpack搭建vue-cli以及webstorm的设置

    N1.检查本地电脑是否安装node和npm 安装vue-cli的前提是已经安装了npm 是否安装npm检查命令 npm -v 如果出现版本号,说明已经安装了npm 查看node版本号 node -v ...

  10. JavaScript文本收缩展开 showdetail

    原文发布时间为:2009-11-15 -- 来源于本人的百度文章 [由搬家工具导入] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tran ...