Word Ladder Problem (DFS + BFS)
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:
- 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"]
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)的更多相关文章
- [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 ...
- 126. Word Ladder II( Queue; BFS)
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- [LeetCode#128]Word Ladder II
Problem: Given two words (start and end), and a dictionary, find all shortest transformation sequenc ...
- 【题解】【字符串】【BFS】【Leetcode】Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- Word Ladder(找出start——end的最短长度)——bfs
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder)
Leetcode之广度优先搜索(BFS)专题-127. 单词接龙(Word Ladder) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...
- Word Ladder系列
1.Word Ladder 问题描述: 给两个word(beginWord和endWord)和一个字典word list,找出从beginWord到endWord之间的长度最长的一个序列,条件: 1. ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
随机推荐
- 发送邮箱验证码、session校验
本篇主要描述“发送邮箱验证码.session校验”相关前(html\js)后(java)台代码,业务逻辑示例,闲话少诉,直接上代码. 1.引入的jar包是mail-1.4.jar 2.java底层发送 ...
- 开发和调试第一个 LLVM Pass
1. 下载和编译 LLVM LLVM 下载地址 http://releases.llvm.org/download.html,目前最新版是 6.0.0,下载完成之后,执行 tar 解压 llvm 包: ...
- python3.X 安装web.py 失败的解决方法
python2.x 安装python是非常顺利的 但是 在进行 pip3 install web.py 时提示很多错误 例如缺少模块 语法错误...... 最后试了一下web.py 的dev版本 pi ...
- 安装cronsun管理定时脚本
1. cronsun 是一个分布式任务系统,单个结点和 *nix 机器上的 crontab 近似.支持界面管理机器上的任务,支持任务失败邮件提醒,安装简单,使用方便,是替换 crontab 一个不错的 ...
- sublime text 3安装 package control 插件的方法
自动安装的方法 - 快捷键ctrl+` 或者View->Show Console,输入如下代码 import urllib.request,os;pf='Package Control.sub ...
- hive新手学习随笔
一.回顾 1.hive基于Hadoop的(存储HDFS,计算MR) 2.sql on hadoop概念 ->简化开发的操作 ->提升 ...
- 基于C语言的面向对象编程
嵌入式软件开发中,虽然很多的开发工具已经支持C++的开发,但是因为有时考虑运行效率和编程习惯,还是有很多人喜欢用C来开发嵌入式软件.Miro Samek说:"我在开发现场发现,很多嵌入式软件 ...
- 7、Linux应用程序地址布局
程序构成 在学习Linux应用程序开发时,经常会遇到如下概念: 代码段.数据段.BSS段(Block Started by Symbol,又名:未始化数据段) .堆(heap)和栈(stack).始化 ...
- Python学习手册之__main__ 模块,常用第三方模块和打包发布
在上一篇文章中,我们介绍了 Python 的 元组拆包.三元运算符和对 Python 的 else 语句进行了深入讲解,现在我们介绍 Python 的 __main__ 模块.常用第三方模块和打包发布 ...
- 找球号(三)南阳acm528(异或' ^ ')
找球号(三) 时间限制:2000 ms | 内存限制:10000 KB 难度:2 描述 xiaod现在正在某个球场负责网球的管理工作.为了方便管理,他把每个球都编了号,且每个编号的球的总个数都 ...