【题目】

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

  1. Only one letter can be changed at a time
  2. 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]的更多相关文章

  1. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  2. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

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

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

  4. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  5. LeetCode :Word Ladder II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

  6. LeetCode: Word Ladder II 解题报告

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

  7. LeetCode: Word Ladder II [127]

    [题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  8. LeetCode Word Ladder 找单词变换梯

    题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...

  9. [leetcode]Word Ladder @ Python

    原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...

随机推荐

  1. Human Gene Functions(dp)

    http://poj.org/problem?id=1080 #include <stdio.h> #include <stdlib.h> #include <strin ...

  2. Gym - 101981D The 2018 ICPC Asia Nanjing Regional Contest D.Country Meow 最小球覆盖

    题面 题意:给你100个三维空间里的点,让你求一个点,使得他到所有点距离最大的值最小,也就是让你找一个最小的球覆盖掉这n个点 题解:红书模板题,这题也因为数据小,精度也不高,所以也可以用随机算法,模拟 ...

  3. protobuf 编译 java js文件详解

    首先下载protobuf.exe 下载地址:https://download.csdn.net/download/qq_34756156/10220137 MessageBody.proto synt ...

  4. 使用TortoiseSVN碰到的几个问题(2)-冲突解决, 图标重载

    8)解决冲突 冲突分为两种:文件冲突---当两名(或更多)开发人员修改了同一个文件中相邻或相同的行时就会发生文件冲突.下面的属性冲突应该也属于文件冲突. 树冲突---当一名开发人员移动.重命名.删除一 ...

  5. C# 如何实现WinForm程序自重启(重新启动自己)

    重启的时间间隔方法 private void Restart() { Thread thtmp = new Thread(new ParameterizedThreadStart(run)); obj ...

  6. 设置Hadoop的 dataNode的单个Map的内存配置

    1.进入hadoop的配置目录 ,找到 环境变量的 $HADOOP_HOME cd $HADOOP_HOME 2.修改dataNode 节点的 单个map的能使用的内存配置 找到配置的文件: /opt ...

  7. Java Web_过滤器

    过滤器分类: Servlet2.5: request:用户直接访问页面时,Web容器将会调用过滤器. forward:目标资源是通过RequestDispatcher的forward访问时,该过滤器将 ...

  8. 小程序组件 Vant Weapp 安装

    文件夹的名称必须是英文 第一步:npm init -y 第二步:npm i vant-weapp -S --production

  9. 启动模拟器的qq

    #coding = utf-8from appium import webdriver '''1.手机类型2.版本3.手机的唯一标识 deviceName4.app 包名appPackage5.app ...

  10. html表单练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...