126. Word Ladder II(hard)
126. 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:
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"]
Return
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
Note:
Return an empty list 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.
解析
For the most voted solution, it is very complicated.
I do a BFS for each path
for example:
{hit} ->
{hit,hot} ->
{hit,hot,dot}/{hit,hot,lot} ->
[“hit”,“hot”,“dot”,“dog”]/[“hit”,“hot”,“lot”,“log”] ->
[“hit”,“hot”,“dot”,“dog”,“cog”],
[“hit”,“hot”,“lot”,“log”,“cog”]
- 牛客网上oj输出顺序
- 思路很清楚,和Word Ladder I类似,只是需要记录输出的路径!
- case通过率为47.22%
- 参考leetcode:https://leetcode.com/problems/word-ladder-ii/discuss/40452
class Solution_126 {
public:
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
vector<vector<string>> res;
unordered_set<string> visit; //notice we need to clear visited word in list after finish this level of BFS
queue<vector<string>> q;
unordered_set<string> wordlist(wordList.begin(), wordList.end());
q.push({ beginWord });
bool flag = false; //to see if we find shortest path
while (!q.empty()){
int size = q.size();
for (int i = 0; i < size; i++){ //for this level
vector<string> cur = q.front();
q.pop();
vector<string> newadd = addWord(cur.back(), wordlist);
for (int j = 0; j < newadd.size(); j++){ //add a word into path
vector<string> newline(cur.begin(), cur.end());
newline.push_back(newadd[j]);
if (newadd[j] == endWord){
flag = true;
res.push_back(newline);
}
visit.insert(newadd[j]); // insert newadd word
q.push(newline);
}
}
if (flag)
break; //do not BFS further
for (auto it = visit.begin(); it != visit.end(); it++)
wordlist.erase(*it); //erase visited one
visit.clear();
}
sort(res.begin(),res.end());
return res;
}
// find words with one char different in dict
// hot->[dot,lot]
vector<string> addWord(string word, unordered_set<string>& wordlist){
vector<string> res;
for (int i = 0; i < word.size(); i++){
char s = word[i];
for (char c = 'a'; c <= 'z'; c++){
word[i] = c;
if (wordlist.count(word)) res.push_back(word);
}
word[i] = s;
}
return res;
}
};
题目来源
126. Word Ladder II(hard)的更多相关文章
- leetcode 127. Word Ladder、126. Word Ladder II
127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...
- [LeetCode] 126. Word Ladder II 词语阶梯之二
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- 126. Word Ladder II
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- Java for LeetCode 126 Word Ladder II 【HARD】
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [LeetCode] 126. Word Ladder II 词语阶梯 II
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- leetcode 126. Word Ladder II ----- java
Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...
- Leetcode#126 Word Ladder II
原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...
- 【leetcode】126. Word Ladder II
题目如下: 解题思路:DFS或者BFS都行.本题的关键在于减少重复计算.我采用了两种方法:一是用字典dic_ladderlist记录每一个单词可以ladder的单词列表:另外是用dp数组记录从star ...
随机推荐
- python - 接口自动化测试 - TestRegister - 注册接口测试用例
# -*- coding:utf-8 -*- ''' @project: ApiAutoTest @author: Jimmy @file: test_register.py @ide: PyChar ...
- Lua 语法要点
table 默认键值都是从1开始 table array = { "A", "B" } array2 = array array[] = "D&quo ...
- loj 300 [CTSC2017]吉夫特 【Lucas定理 + 子集dp】
题目链接 loj300 题解 orz litble 膜完题解后,突然有一个简单的想法: 考虑到\(2\)是质数,考虑Lucas定理: \[{n \choose m} = \prod_{i = 1} { ...
- 几种API接口
实用号码归属地查询(IP 地址,手机号码): 默认格式: http://api.liqwei.com/location/ (使用来访者的 IP 地址) 指定 IP 地址格式: http://api.l ...
- redis在linux上的安装和配置
https://blog.csdn.net/lzding/article/details/52040501(直接可以用的安装phpredis) http://www.runoob.com/redis/ ...
- 【04】react 之 复合组件
1.1. 什么是组件? 前端开发中组件也称为UI组件,组件即将一段或几段完成各自功能的代码段封装为一个或几个独立的部分.UI组件包含了这样一个或几个具有各自功能的代码段,最终完成了用户界面的表示.R ...
- iOS 初始化(init、initWithNibName、initWithCoder、initWithFrame)
很多朋友如果是初学iOS开发,可能会被其中的几个加载方法给搞得晕头转向的,但是这几个方法又是作为iOS程序员必须要我们掌握的方法,下面我将对这几个方法做一下分析和对比,看看能不能增加大家对几个方法的理 ...
- favicon还是这个网站生成的比较正确
原文发布时间为:2011-11-16 -- 来源于本人的百度文章 [由搬家工具导入] http://tools.dynamicdrive.com/favicon/ http://www.rw-desi ...
- .NET抓取数据范例 抓取页面上所有的链接
原文发布时间为:2009-11-15 -- 来源于本人的百度文章 [由搬家工具导入] .NET抓取数据范例 抓取页面上所有的链接 前台: <%@ Page Language="C#&q ...
- c#中使用事务
原文发布时间为:2009-04-14 -- 来源于本人的百度文章 [由搬家工具导入] 问:为什么要用事务? 答:事务保证要么一组操作执行成功,要么全不执行。。。。 /// <summary> ...