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. 异常笔记:Hadoop异常 namenode.NameNode: Encountered exception during format

    00:53:47,977 WARN namenode.NameNode: Encountered exception during format: java.io.IOException: Canno ...

  2. axios和ajax,fetch的区别

    1,传统 Ajax 指的是 XMLHttpRequest(XHR), 最早出现的发送后端请求技术,隶属于原始js中,核心使用XMLHttpRequest对象,多个请求之间如果有先后关系的话,就会出现回 ...

  3. day 18 类与类之间的关系

    类与类之间的关系     在我们的世界中事物和事物之间总会有一些联系.    在面向对象中,类和类之间也可以产生相关的关系 1.依赖关系     执行某个动作的时候. 需要xxx来帮助你完成这个操作, ...

  4. Dubbo client 启动报错:No provider available for the service use dubbo version 2.5.3

    1.异常 java.lang.IllegalStateException: Failed to check the status of the service org.ko.server.servic ...

  5. Python学习3——变量如何存储数据

    数值类型:包括整型.浮点型 变量名字代表的是存储地址. num01 = 100 print(id(num01)) #输出变量num01存储的内存地址,输出的是十进制值 num02 = num01 pr ...

  6. docker-compose入门示例:一键部署 Nginx+Tomcat+Mysql

    整体环境配置 整体环境的配置,如果一个一个 Dockerfile 去写,那么是相当麻烦的,好在 Docker 有一个名为 Docker-Compose 的工具提供,我们可以使用它一次性完成整体环境的配 ...

  7. 查看dll 的是32位还是64位

    1. dumpbin 命令 ** dumpbin 路径写入环境变量 ***  使用 dumpbin /headers 文件名去查看 [X86 表示32位  x64表示64位] 2. 使用 DEPEND ...

  8. BZOJ:2763-[JLOI2011]飞行路线(最短路分层图)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2763 解题心得: 第一次见到分层最短路.其实题中说选择k条路径免费,那怎么选k条路径并没 ...

  9. services 系统服务的启动、停止、卸载

    MySQL服务的启动.停止与卸载 在 Windows 命令提示符下运行: 启动: net start MySQL 停止: net stop MySQL 卸载: sc delete MySQL Sc d ...

  10. git忽略项gitegnore配置

    在git中如果想忽略掉某个文件, 不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法.这个文件每一行保存了一个匹配的规则 例如 # 此为注释 – 将被 Git 忽略 *.a # ...