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 ...
随机推荐
- [SHELL]软件管理
- js中定时器使用方法经验总结
前言,最近在做一个音频播放项目的时候,碰到播放时间精度的问题,捣鼓了几天,最终巧妙的运用定时器去降低了错误发生频率 正题,下面是对定时器的使用总结,如有错误之处,请读者加以纠正. 延迟执行(1次) s ...
- CSS动画实例
上一篇讲过css动画transform transition的语法,这一节展示自己做的几个小例子加深印象 1. 线条动画效果 代码:最外层div包含2个小的div : a和b. a有左右边框(高度 ...
- Python-变量与基础数据类型
·变量(variable) 笔记: 变量本质上是一个占位符.变量可以用来存储整数.字符串.列表等.简单的可以理解为一个座位,可以坐老人也可以坐小孩,可以坐男孩,也可以坐女孩. 在python里,标识 ...
- python3 练习题100例 (六)
题目六:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.……. #!/usr/bin/env python3 ...
- docker 环境下创建 overlay 网络方案
一.环境 三台机器,其中一台安装 consul(192.168.1.21), 两台创建网络(192.168.1.32,33) 二.实现步骤 1.构建环境 1)三台机器部署docker环境 2)选择一台 ...
- C语言顺序表
顺序表结构可设为一个数组和一个指向尾部的变量,数组用来存放元素,指向尾部的变量在插入元素的时候加一,删除元素的时候减一,始终指向尾部. typedef int elemtype; typedef st ...
- centos7关闭图形界面启动系统
手动敲那么多不累么?仅2条命令(好) 1,命令模式systemctl set-default multi-user.target 2,图形模式systemctl set-default graphic ...
- go内建容器-Map
1.基础定义 golang中的map如同它的函数一样"纯粹",map就是用来存储键值对的容器,别管什么哈希不哈希的(底层已实现),用就行 //创建一个map m := map[st ...
- cocos2d中锚点概念
这两天看了下锚点的概念. /** * Sets the anchor point in percent. * * anchorPoint is the point around which all t ...