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. python week09 Mysql 数据库基础知识

    第一篇:初识数据库 注:<基础概念,不再赘述,点开链接查看> 第二篇:库相关操作 一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些 ...

  2. Leetcode 611.有效三角形的个数

    有效三角形的个数 给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数. 示例 1: 输入: [2,2,3,4] 输出: 3 解释: 有效的组合是: 2,3,4 (使用第一个 ...

  3. Python多进程之multiprocessing模块和进程池的实现

    1.利用multiprocessing可以在主进程中创建子进程,提升效率,下面是multiprocessing创建进程的简单例子,和多线程的使用非常相似 ''' 代码是由主进程里面的主线程从上到下执行 ...

  4. Python面向对象之常用的特殊方法(5)

    Python面向对象里面有很多特殊方法,例如__init__(构造方法),__del__(析构方法),这些方法对于面向对象编程非常重要,下面列出一些常用的特殊方法 (1)__call__ class ...

  5. koa2在node6中如何运行

    koa2在node6下运行 { "babel-core": "^6.24.1", "babel-plugin-syntax-async-functio ...

  6. 利用js生成二维码

    $('#barcode').qrcode({ width: 300, height: 300, render: !!document.createElement('canvas').getContex ...

  7. linux系统ssh远程连接检查脚本

    脚本用于检查Linux系统云服务器出现的常见远程不能连接问题,脚本可以提前放到服务器里,出现问题时可以web vnc登陆上去执行试试. 附:管理控制台终端web vnc 方式登录,参考:http:// ...

  8. 软件包管理rpm_yum

    和文本相关的命令cat 正向显示文本tac 反向显示文本more 可以一步一步显示文本文件less 还可以往上看.几个快捷键:j(往下看), k (往上看), g(定位最上), G(定位最下), ct ...

  9. NOIP2017赛前模拟10月30日总结

    题目1: n个人参赛(n<=100000),每个人有一个权值··已知两个人权值绝对值之差小于等于K时,两个人都有可能赢,若大于则权值大的人赢···比赛为淘汰制,进行n-1轮·问最后可能赢的人有多 ...

  10. php中的对象赋值

    如果现在问你一个问题: <?php class A{ public $a = 1; } $a = new A(); $b = $a; $b->a = 3; print_r($a->a ...