【题目】

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. 判断ascii码是什么的函数

    function CharMode(iN){ if (iN>=48 && iN <=57) //数字 return 1; if (iN>=65 && ...

  2. mysql行列转置

    --创建行转列表及插入数据 create table tb_RowConvertToColumn ( username nvarchar(100) null, course nvarchar(100) ...

  3. Oracle_exp/expdp备份

    目录索引 1.exp和expdp的区别 2.expdp导出数据库流程 一.↓↓exp和expdp的区别↓↓ 1.exp和expdp最明显的区别就是导出速度的不同.expdp导出是并行导出(如果把exp ...

  4. windows服务安装错误 在‘安装’过程发生异常:System.ComponentModel.Win32Exception:系统正在关机

    今天安装windows服务的时候先是在本地安装测试通过,但是一到服务器就一直安装失败 在‘安装’过程发生异常:System.ComponentModel.Win32Exception:系统正在关机 然 ...

  5. UML基本关系

    UML-Unified Model Language 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言.UML的定义包括UML语义和UML表示法两个元素. UML是在开发阶 ...

  6. 前端编码规范(2)HTML 规范

    文档类型 推荐使用 HTML5 的文档类型申明: <!DOCTYPE html> (建议使用 text/html 格式的 HTML.避免使用 XHTML.XHTML 以及它的属性,比如 a ...

  7. JAVA软件工程师应该具备的技能有哪些?

    前言:有朋友问我:学历和能力哪个重要?我个人觉得能力大于学历,没有能力哪来的学历,学历只是证明能力的一方面.为此在能力方面畅谈java软件工程师必备的能力.作为一名合格的java工程师,不仅需要学历, ...

  8. QT-Creator+SDK+编译器+自定义配置

    QT4.8的软件曾经耗费巨大的功夫进行构建,不舍得扔掉!重新安装Qt4.8版本 1.安装qt-creator 安装qt-creator-win-opensource-2.4.0.exe版本,不建议使用 ...

  9. 范畴论-一个单子(Monad)说白了不过就是自函子范畴上的一个幺半群而已

    范畴即为结构:包含要素和转化. 范畴为高阶类型. 函子为高阶函数.函子的输入为态射.函子为建立在态射基础上的高阶函数.函子用于保持范畴间映射的结构.态射用于范畴内部的转换. 群为运算规则的约束. 自函 ...

  10. drf05 路由Routers

    对于视图集ViewSet,我们除了可以自己手动指明请求方式与动作action之间的对应关系外,还可以使用Routers来帮助我们快速实现路由信息. REST framework提供了两个router ...