https://leetcode.com/problems/word-ladder-ii/

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

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Return

  [
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
class Solution {
public:
void dfs(vector<vector<string> >& res, unordered_map<string, vector<string> >& fa, vector<string> load, string beginWord, string curWord) {
if(curWord == beginWord) {
reverse(load.begin(), load.end());
res.push_back(load);
reverse(load.begin(), load.end());
return;
} for(int i=; i<fa[curWord].size(); ++i) {
load.push_back(fa[curWord][i]);
dfs(res, fa, load, beginWord, fa[curWord][i]);
load.pop_back();
}
} vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) {
vector<vector<string> > res;
if(beginWord.compare(endWord) == ) return res; wordList.insert(endWord); unordered_map<string, vector<string> > fa;
unordered_set<string> vis;
unordered_set<string> lev;
unordered_set<string> next_lev; lev.insert(beginWord);
bool found = false; while(!lev.empty() && !found) {
if(lev.find(endWord) != lev.end()) found = true; for(auto str: lev) vis.insert(str); for(auto str : lev) {
for(int i=; i<str.length(); ++i) {
for(char ch = 'a'; ch <= 'z'; ++ch) {
if(str[i] != ch) {
string tmp = str;
tmp[i] = ch;
if(wordList.find(tmp) != wordList.end() && vis.find(tmp) == vis.end()) {
next_lev.insert(tmp);
fa[tmp].push_back(str);
}
}
}
}
} lev.clear();
swap(lev, next_lev);
} if(found) {
vector<string> load;
load.push_back(endWord);
dfs(res, fa, load, beginWord, endWord);
} return res;
}
};

leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)的更多相关文章

  1. [LeetCode] 126. Word Ladder II 词语阶梯 II

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

  2. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  3. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  4. [LeetCode] 126. Word Ladder II 词语阶梯之二

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

  5. leetcode 126. Word Ladder II ----- java

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

  6. Leetcode#126 Word Ladder II

    原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...

  7. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  8. 126. Word Ladder II(hard)

    126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...

  9. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

随机推荐

  1. CSRF攻击原理解析与对策研究

    1.引言       跨站点请求伪造(Cross—Site Request Forgery).以下简称CSRF.是一种广泛存在的网站漏洞.Gmail.YouTube等著名网站都有过CSRF漏洞.甚至包 ...

  2. 几款国产开源的Windows界面库

    上次介绍的几款图形界面库http://blog.okbase.net/vchelp/archive/23.html都是国外的开源项目,今天介绍的几款都是国人的开源项目,大部分是采用DirectUI设计 ...

  3. C++模板使用介绍

    1. 模板的概念. 我们已经学过重载(Overloading),对重载函数而言,C++的检查机制能通过函数参数的不同及所属类的不同.正确的调用重载函数.例如,为求两个数的最大值,我们定义MAX()函数 ...

  4. JAVA面试题:Spring中bean的生命周期

    Spring 中bean 的生命周期短暂吗? 在spring中,从BeanFactory或ApplicationContext取得的实例为Singleton,也就是预设为每一个Bean的别名只能维持一 ...

  5. 【转】windows c++获取文件信息——_stat函数的使用

    _stat函数的功能 _stat函数用来获取指定路径的文件或者文件夹的信息. 函数声明 int _stat( const char *path, struct _stat *buffer ); 参数: ...

  6. 面试大总结:Java搞定面试中的链表题目总结

    package LinkedListSummary; import java.util.HashMap; import java.util.Stack; /** * http://blog.csdn. ...

  7. SGU 101

    SGU 101,郁闷,想出来算法,但是不知道是哪个地方的问题,wa在第四个test上. #include <iostream> #include <vector> #inclu ...

  8. Linux信号列表

    我们运行如下命令,可看到Linux支持的信号列表: ~$ kill -l1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL5) SIGTRAP 6) SIGABRT 7) ...

  9. WINCE6.0去掉桌面快捷方式

    WINCE6.0去掉桌面快捷方式,主要是修改xxx.bat文件,比如我要去掉My Documents和Media Player的快捷方式. (1)    去掉My Documents桌面快捷方式 找到 ...

  10. 自定义View 实现软键盘实现搜索

    1. xml文件中加入自定义 搜索view <com.etoury.etoury.ui.view.IconCenterEditText android:id="@+id/search_ ...