【题目】

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. js中 if不判断解决方式

    $(function() { $("#number").blur(function() { var number = $('#number').val(); var num = $ ...

  2. Vue发布过程中遇到坑,以及webpack打包优化

    前言 这段时间,本人自己做了一个vue画面部署到自己的服务器上,发现运行速度慢的的惊人,虽然服务器很渣(本人没什么钱,只能租最差的服务器,主要是给自己学习用的),但是这样开发出来的网站简直不能用,所以 ...

  3. MySql c#通用类 转

    using System;using System.Data;using System.Configuration;using System.Collections.Generic;using Sys ...

  4. html5与css3入门知识点精炼

    <meta name = "keywords" content="…………"/>(网页搜索时要输入的关键字) <meta name = &qu ...

  5. React+Dva

    Reducer reducer 是一个函数,接受 state 和 action,返回老的或新的 state .即:(state, action) => state Effect app.mode ...

  6. 努比亚(nubia) V18 NX612J 解锁BootLoader 并刷入recovery ROOT

    recovery制作来自绯色玻璃 努比亚(nubia) V18 NX612J 解锁BootLoader 并刷入recovery ROOT 工具下载链接:https://pan.baidu.com/s/ ...

  7. iproute2和tc的高级路由用法

    #Linux advanced router ip link show #显示链路 ip addr show #显示地址(或ifconfig) ip route show #显示路由(route -n ...

  8. 备份xx

    https://www.tuicool.com/articles/V3EBzev https://www.tuicool.com/topics/11080087?st=0&lang=1& ...

  9. Hadoop-2.2.0在Unbuntu ADM64中需要重新编译Native Lib

    通过:cat /etc/issue 查看当前系统版本: Ubuntu 12.04.3 通过:uname -ar 查看更想起信息: Linux ubuntu-236 3.8.0-29-generic # ...

  10. 【转】虚拟化(五):vsphere高可用群集与容错

    vsphere高级功能需要vcenter server和共享存储的支持才能实现.vsphere的高级功能有 vmotion.storage vmotion.vsphere HA.vsphere DRS ...