【题目】

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. BZOJ 4800 折半暴搜

    思路: 把它拆成两半  分别搜一发 两部分分别排好序 用two-pointers扫一遍 就可以了. (读入也要用long long) //By SiriusRen #include <cstdi ...

  2. MySQL学习笔记之内连接

    不多说,直接上干货!  MySQL的内连接 #内连接,两个表按照条件匹配 select class1.stuid,class1.stuname,class1.sex,course from class ...

  3. Java上传视频

    页面: 上传文件时的关键词:enctype="multipart/form-data" <%@ page language="java" import=& ...

  4. asp.net 后台注册脚本

    string myScript = "function ShowPanel() { $('.nav a[href=\"#" + PanelType.wenben.ToSt ...

  5. ubuntu 14.04安装x11VNC

    环境:Ubuntu 14.04, 1)安装x11vnc: sudo apt-get install x11vnc 2)设置VNC的连接密码: x11vnc -storepasswd Enter VNC ...

  6. 详解CorelDRAW智能填充工具的运用

    使用智能填充工具可以为任意的闭合区域填充颜色并设置轮廓.与其他填充工具不同,智能填充工具仅填充对象,它检测到区域的边缘并创建一个闭合路径,因此可以填充区域.例如,智能填充工具可以检测多个对象相交产生的 ...

  7. CF1168B Good Triple 性质分析_好题

    题意翻译 给出01串s,求数对[l,r]个数,使得能找到至少一对[x,k],使1<=x,k<=|s|且l<=x<x+2k<=r且s[x]=s[x+k]=s[x+2k] 题 ...

  8. Clocksource tsc unstable

    内核在启动过程中会根据既定的优先级选择时钟源.优先级的排序根据时钟的精度与访问速度. 其中CPU中的TSC寄存器是精度最高(与CPU最高主频等同),访问速度最快(只需一条指令,一个时钟周期)的时钟源, ...

  9. Ubuntu安装RTX2080显卡驱动

    安装RTX2080显卡驱动 近日新购了一台DELL服务器,用于TensorFlow,由于显卡是另加的,需要安装显卡驱动. 服务器配置 服务器型号:DELL PowerEdge R730 CPU:2*I ...

  10. 绝对好用的浏览器json解析网址

    你们是否经常在浏览器输入请求地址解析遇到中文乱码的情况,今天我找到了一个好用的浏览器解析json网址,绝对好用. 1.直接输入网址 http://pro.jsonlint.com/ 2.输入要解析的j ...