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

  1. Only one letter can be changed at a time.
  2. 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"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Note:

  • Return 0 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.

Code:

 class Solution {
public:
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
//1.convert vector to unordered_set
unordered_set<string> wordDict;
for(int i=; i<wordList.size(); i++)
{
wordDict.insert(wordList[i]);
}
if(wordDict.find(endWord) == wordDict.end()) return vector<vector<string>>(); //2.record each node's pre_node from begin to end using bfs strategy
unordered_map<string, vector<string>> preNode;
bfs(preNode, wordDict, beginWord, endWord); //3.search all the road using dfs from end to start
vector<vector<string>> res;
vector<string> temp;
dfs(beginWord, endWord, temp, preNode, res); return res;
} private:
void bfs(unordered_map<string, vector<string>>&preNode,
unordered_set<string>& wordDict, string beginWord, string endWord)
{
queue<string> q;
unordered_set<string> visit;
visit.insert(beginWord);
vector<string> connect;
q.push(beginWord);
while(!q.empty())
{
int len = q.size();
vector<string> tmpVisit;
while(len--)
{
string current = q.front();
q.pop();
isConnect(connect, wordDict, current, endWord, visit);
for(int i=; i<connect.size(); i++)
{
if(visit.find(connect[i]) == visit.end()) // not visited
{
if(preNode[connect[i]].empty())
{
tmpVisit.push_back(connect[i]);
q.push(connect[i]);
}
preNode[connect[i]].push_back(current);
}
}
} //each level
for(int j=; j<tmpVisit.size(); j++)
{
visit.insert(tmpVisit[j]);
}
if(visit.find(endWord) != visit.end())
return;
}
} void isConnect(vector<string>& connect, unordered_set<string>& wordDict,
const string& current, const string& end, unordered_set<string>& visit)
{
connect.clear();
string cur = current;
for(int i=; i<cur.size(); i++)
{
char t = cur[i];
for(char c='a'; c<'z'; c++)
{
if(c == t) continue;
cur[i] = c;
if((wordDict.find(cur) != wordDict.end()) && visit.find(cur) == visit.end())
{
connect.push_back(cur);
}
}
cur[i] = t;
}
} void dfs(const string& beginWord, const string& t, vector<string> tmp,
unordered_map<string, vector<string>>& preNode, vector<vector<string>>& res)
{
if(t == beginWord)
{
tmp.push_back(beginWord);
vector<string> tmpres(tmp.rbegin(), tmp.rend());
res.push_back(tmpres);
return;
}
tmp.push_back(t);
for(int i=; i<preNode[t].size(); i++)
{
dfs(beginWord, preNode[t][i], tmp, preNode, res);
}
}
};

Word Ladder Problem (DFS + BFS)的更多相关文章

  1. [LeetCode] 127. Word Ladder _Medium tag: BFS

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  2. 126. Word Ladder II( Queue; BFS)

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

  3. [LeetCode#128]Word Ladder II

    Problem: Given two words (start and end), and a dictionary, find all shortest transformation sequenc ...

  4. 【题解】【字符串】【BFS】【Leetcode】Word Ladder

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

  5. Word Ladder(找出start——end的最短长度)——bfs

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  6. Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder)

    Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

  7. Word Ladder系列

    1.Word Ladder 问题描述: 给两个word(beginWord和endWord)和一个字典word list,找出从beginWord到endWord之间的长度最长的一个序列,条件: 1. ...

  8. [LeetCode] Word Ladder 词语阶梯

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

  9. LeetCode:Word Ladder I II

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

随机推荐

  1. 如何利用Linux去油管下载带字幕的优质英文资料提升英文听力和词汇量

    非常方便地从油管下载你需要的任何英文视频资料,并且带字幕,方便你学习某个特定领域的词汇: [step1,Centos6系统安装youtbe-dl下载带英文字幕的视频] 1.首先需要安装youtube- ...

  2. TinyMCE插件:FileManager [4.x-6.x] 配置及BUG处理

    FileManager最新版已升级到9.x,9.x新增了对文件的批量处理,但仍然有部分同学在继续使用6.x,这里大叔整理了一份自己在配置6.x时,遇到的问题和解决方案. 安装 下载安装包解压后,在根目 ...

  3. 如何分析Mysql慢SQL

    内容摘要: 开启慢查询日志捕获慢SQL 使用explain分析慢SQL 使用show profile查询SQL执行细节 常见的SQL语句优化 一.开启慢查询日志捕获慢SQL ① 查询mysql是否开启 ...

  4. select epoll poll

    如何理解 Epoll select 和 poll 三种模型,能否用生活中的例子做比喻? 比如说你从某宝下单买了几个东西,这几个东西分别由N个快递员分别给你送过来.在某一时刻,你开始等快递.对于sele ...

  5. 抓猫_KEY

    抓猫 题面如下: [ 题目描述] 流浪猫布满城市的每一个角落, 非常影响市容市貌, 作为城市聘请的抓猫者, 你有一 种捕捉器, 一定可以捕捉到所有走到里面的猫, 更加幸运的是你有一个非常厉害的动物心理 ...

  6. 为啥学蛇和python10年后的变化

    作者:cheng rianley链接:https://www.zhihu.com/question/22112542/answer/166053516来源:知乎著作权归作者所有.商业转载请联系作者获得 ...

  7. Ceres优化

    Ceres Solver是谷歌2010就开始用于解决优化问题的C++库,2014年开源.在Google地图,Tango项目,以及著名的SLAM系统OKVIS和Cartographer的优化模块中均使用 ...

  8. ORB-SLAM(十)LoopClosing

    构造函数 LoopClosing(Map* pMap, KeyFrameDatabase* pDB, ORBVocabulary* pVoc,const bool bFixScale); 主要分两部分 ...

  9. Altium designer18设置原理图尺寸

    1. AD18版本设置原理图尺寸和以前版本不一样,具体是在界面右侧Properties里面的Sheet Sizes.

  10. Win10 远程服务器版

    朋友的电脑刚装了1803版的Win10,然后他用KMS_VL_ALL6.9激活了一下,竟然变成了一个奇怪的版本:“远程服务器版”!第一次见这玩意,还真稀罕.帮他研究了一下,发现KMS_VL_ALL在激 ...