LeetCode: Word Ladder [126]
【题目】
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
- Only one letter can be changed at a time
- Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
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.
【题意】
给定两个单词start和end, 一个词典,要求找出start到end的最短转换序列。
要求转换过程中每次仅仅能改变一个字符,转换的中间单词必须在词典中。
有三点说明:
1. 假设没有转换序列,则返回0
2. 起始单词以及字典中的全部单词的长度都是相等的
3. 全部单词中的单词都保证小写。
【思路】
最直接的办法是先找出start和词典中每一个单词在序列中全部合法的next(下一个)单词集合。事实上就是构造了一个邻接矩阵。然后邻接矩阵上跑BFS,计算最小的转换层次,即为本题要求的最少的转换次数。这样的的复杂度是O(n^2),显然这么做就跪了。
我们不用傻逼呵呵的去建图。吃力不讨好。我们仅仅须要针对序列每一个位置上的词进行考察就能够了,
首先看start, start每一位分别用a~z(除了自有的字母)替换,然后从词典中找出一次转换可达的单词集合,把这些一次可达的单词从词典中删除。
然后跟start同样,我们对一次转换可达的每一个单词值的每一位分别用a~z(除了自有的字母)替换。然后从词典中找出二次可达的单词集合,把这些二次可达的单词从词典中删除。
反复上面的过程,我们能够找到3次,4次。5次...转换可达的单词。
在转换过程中推断转换后的结果是否是end。假设已经能转换成end,则直接返回当前转换次数就可以。
因为单词长度一定。如果为K,我们用a-z来替换每一位的方法来寻找后继单词,即使运气背到家没有这种转换序列。我们把左右的单词都推断了一遍,,复杂度也仅仅有O(26Kn),比O(n^2)快非常多。
【代码】
class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
if(start==end)return 1;
int level=0;
queue<string> q1;
queue<string> q2;
//初始化q1
q1.push(start);
level=1;
while(!q1.empty()||!q2.empty()){
level++;
if(!q1.empty()){
while(!q1.empty()){
string word=q1.front(); q1.pop();
//确定转换序列中的后继单词
for(int i=0; i<word.length(); i++){
string tword=word;
for(char c='a'; c<='z'; c++){
if(tword[i]!=c){
tword[i]=c;
//推断是否是end
if(tword==end)return level;
//推断后继单词是否在dict中,假设在,即合法,增加到q2中
if(dict.find(tword)!=dict.end()){
q2.push(tword);
dict.erase(tword);
}
}
}
}
}
}
else{
while(!q2.empty()){
string word=q2.front(); q2.pop();
//确定转换序列中的后继单词
for(int i=0; i<word.length(); i++){
string tword=word;
for(char c='a'; c<='z'; c++){
if(tword[i]!=c){
tword[i]=c;
//推断是否是end
if(tword==end)return level;
//推断后继单词是否在dict中。假设在。即合法。增加到q1中
if(dict.find(tword)!=dict.end()){
q1.push(tword);
dict.erase(tword);
}
}
}
}
}
}
}
return 0;
}
};
LeetCode: Word Ladder [126]的更多相关文章
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- [leetcode]Word Ladder @ Python
原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...
随机推荐
- [Apple开发者帐户帮助]四、管理密钥(2)获取密钥标识符
创建JSON Web令牌(JWT)以与启用的服务进行通信时,需要密钥标识符. 获取密钥标识符 在“ 证书,标识符和配置文件”中,选择侧栏中“键”下的“全部”. 在右侧,选择私钥. 密钥标识符显示在密钥 ...
- c# 官方文档必看
https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-version-history
- Hibernate搭建框架(一)
什么是hibernate? hibernate是一个orm框架,实现了对JDBC的封装.通过xml文件来实现类和表之间的映射,这样就可以使用操作对象的方式来操作数据库. 官网:http://hiber ...
- Lua eval实现
因为loadstring总是在全局环境中编译它的串,所以编译出的函数访问的变量是全局变量.为了避免污染全局环境我们需要用setfenv修改函数的环境 function eval(equation, v ...
- angularJS之ng-bind与ng-bind-template的区别
ng-bind-template 指令用于告诉 AngularJS 将给定表达式的值替换 HTML 元素的内容. 当你想在 HTML 元素上绑定多个表达式时可以使用 ng-bind-template ...
- 4星|《OKR实践指南》:老司机经验谈
OKR 实践指南:知乎任向晖.雷明灿作品 (知乎「一小时」系列) 作者所在的公司已经实施了OKR十个季度了.算是目前少有的OKR老司机.书中介绍的是作者的实践经验,在目前的OKR中文书中这本算是比较少 ...
- Centos6.6 安装Redis
一.介绍 redis在做数据库缓存,session存储,消息队列上用的比较多 二.安装 $ yum install -y wget gcc make tcl $ wget http://downloa ...
- iOS https 证书链获取
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)chall ...
- 虚拟DOM介绍
[转自]:https://www.jianshu.com/p/616999666920 为什么需要虚拟DOM 先介绍浏览器加载一个HTML文件需要做哪些事,帮助我们理解为什么我们需要虚拟DOM.web ...
- [Jxoi2012]奇怪的道路 题解(From luoguBlog)
题面 状压好题 1<= n <= 30, 0 <= m <= 30, 1 <= K <= 8 这美妙的范围非状压莫属 理所当然地,0和1代表度的奇偶 dp[i][j ...