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. go加密算法:非对称加密(二)--Hash

    关于一些加密算法的应用和信息,可以在以下博客中查找到: https://www.cnblogs.com/charlesblc/p/6130141.html // MyHash package main ...

  2. thinkphp 5.1/tp5.1 route路由bug

    tp5.1下面RuleItem类中,match方法. 如果同一个控制器下面,写了两个路由,后一个路由比包含前一个路由,则访问后一个路由地址的时候,会跳转到前面定义的那个路由

  3. 浅谈fail-fast机制

    fail-fast机制即为快速失败机制,个人认为是一种防护措施,在集合结构发生改变的时候,使尽全力抛出ConcurrentModificationException,所以该机制大部分用途都是用来检测B ...

  4. FPGA算法学习(1) -- Cordic(Verilog实现)

    上两篇博文Cordic算法--圆周系统之旋转模式.Cordic算法--圆周系统之向量模式做了理论分析和实现,但是所用到的变量依然是浮点型,而cordic真正的用处是基于FPGA等只能处理定点的平台.只 ...

  5. ant-design form

    表单配置 示例代码 import { Form } from 'antd'; const FormItem = Form.Item; class NormalLoginForm extends Rea ...

  6. 【blockly教程】Blockly编程案例

    案例一 原码反码和补码  我们把一个数在计算机内被表示的二进制形式称为机器数,该数称为这个机器数的真值.机器数有固定的位数,具体是多少位与机器有关,通常是8位或16位.原码:是指符号位用0或1表示,0 ...

  7. 说说CakePHP的关联模型之一 基本关联

    一个无论多么复杂的程序,拆开看无非是三种逻辑结构的组合:顺序结构.条件结构和循环结构. 类似的,数据库中表与表的之间的关联无外乎四种:一对一.一对多.多对一和多对多. CakePHP的模型层中定义了四 ...

  8. javasript 字符串 数组操作

    Javascript中经常涉及到对字符串和数组的处理,今天总结一下具体的用法 一 操作字符串 String对象有很多函数,可以以不同的方式访问和操作字符串,具体方法如下:   charAt(index ...

  9. Unity Container中的几种注册方式与示例

    1.实例注册 最简单的注册方式就是实例注册,Unity 容器负责维护对一个类型的单例引用,比如: 有如下的实际类型: namespace ConsoleSample { public class Sa ...

  10. hdu1045Fire Net(经典dfs)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...