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. 正则表达式re模块的详解-python

    1.元字符([ ]),它用来指定一个character class.所谓character classes就是你想要匹配的字符(character)的集合.字符(character)可以单个的列出,也 ...

  2. JQuery向ashx提交中文参数方案 [转]

    转自:http://blog.csdn.net/wangqiuyun/article/details/8450964 字符编码这个东西,一旦和中文打上交道就不可避免出现乱码,今天项目用到了JQuery ...

  3. mojoportal在IE10中点击ImageButton出错的处理方法

    在ie10中,如果点击了mojoportal中的imagebutton,会出现错误,在ie10之前的浏览器,及ie10的兼容模式中及谷歌浏览器中都不会出现. 日志中 错误信息如下: 2013-09-2 ...

  4. UVALive 5983 MAGRID DP

    题意:在一个n*m的网格上,从(0,0)走到(n-1,m-1),每次只能向右或者向下走一格.一个人最初有一个生命值x,走到每一个格生命值会 变为x + s[i][j],(s[i][j]可为负,0,正) ...

  5. [luoguP2596] [ZJOI2006]书架(splay)

    传送门 题目中的几个操作,直接splay搞一下即可: 把s旋转到根,左子树接到右子树 把s旋转到根,右子树接到左子树 交换s相邻的信息即可 把s旋转到根,左子树的大小即为答案 找第k大 没了 #inc ...

  6. openssl Rsa 分段加密解密

    密钥长度 1024 openssl genrsa -out rsa_private_key.pem openssl rsa -in rsa_private_key.pem -pubout -out r ...

  7. 图片抓取器web + winform

    原文发布时间为:2009-11-21 -- 来源于本人的百度文章 [由搬家工具导入] 请先学习:http://hi.baidu.com/handboy/blog/item/bfef61000a67ea ...

  8. 极致 Web 性能 —— SPA 性能指南

    前言 前端框架时代,为开发体验.效率与页面性能带来,非常大的革命.大家纷纷拿起一系列打包工具(webpack/parcel etc.),配合一系列加载器快速搭建起一个 SPA 页面. SPA 应用带来 ...

  9. IMAGE_OPTIONAL_HEADER32 结构作用

    IMAGE_OPTIONAL_HEADER32 结构作用 接 着我们来谈谈 IMAGE_OPTIONAL_HEADER 结构,正如名字的意思,这是一个可选映像头,是一个可选的结构,但是呢,实际上上节课 ...

  10. Berkeley DB Java Edition 简介

    一.             简介        Berkeley DB Java Edition (JE)是一个完全用JAVA写的,它适合于管理海量的,简单的数据. l         能够高效率的 ...