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 length5.

Note:

    • Return 0 if there is no such transformation sequence.
    • All words have the same length.
    • All words contain only lowercase alphabetic characters.

题意:从一个词变为另一个词,每次只能变化一个字符,变化的中间词都在字典中,求最短的路径。

思路:图论中单源最短路,求单源最短路比较通用算法是BFS和Dijkstra,其区别在于BFS不能用于带权重图,而Dijkstra可以。另外,BFS和Dijkstra的区别是前者的时间复杂度是O(n),后者最多优化到O(mlogn),如果条件成立一般选择BFS。本题中,两字符串之间是无权重的,连通是1,不连通是无穷,故本题采用BFS方法。

首先,要进行图的映射,顶点是每个字符串,若是两个字符串相差1个字符且后者在dict中,就相连构成边;然后,将起点加入到队列中,遍历和其相差为1的字符串,将其加入到队列中,直到找到目标字符串(找不到就返回0)。这里值得注意的是:寻找和某一个字符相差1的字符串的方式,不是遍历字典中的每一个字符串,这样当字典中元素较多时,将会耗时严重。这次采用,对该字符串的每个位置上的字符,用26个字母进行替换,找到和其相差为1的所有字符串;还有一点值得注意的是,每次在字典中找到某一个字符串以后,要在字典中删除该字符串,这样可以避免重复查找(详细说明见这里)。以题中的例子为例,见下图:

如上图,每一层依次加入到队列中,只要先找到目标词语,就返回此时的值就行。参考了这里,代码如下:

 class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict)
{
queue<pair<string,int>> que;
que.push(make_pair(start,));
dict.erase(dict.find(start)); while( !que.empty())
{
auto val=que.front();
que.pop();
if(val.first==end) return val.second; for(int i=;i<val.first.size();++i)
{
string str=val.first;
for(int j=;j<;++j)
{
str[i]='a'+j;
if(dict.find(str) !=dict.end())
{
que.push(make_pair(str,val.second+));
dict.erase(str);
}
}
}
}
return ;
}
};

思路是一样的,换一种写法;

 class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict)
{
unordered_map<string,int> m;
queue<string> que;
m[start]=;
que.push(start); while( !que.empty())
{
string val=que.front();
que.pop(); for(int i=;i<val.size();++i)
{
string str=val;
for(int j=;j<;++j)
{
str[i]='a'+j;
if(dict.count(str)&&str==end)
return m[val]+;
if(dict.count(str)&& !m.count(str))
{
que.push(str);
m[str]=m[val]+;
}
}
}
}
return ;
}
};

[Leetcode] word ladder 单词阶梯的更多相关文章

  1. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  2. [LeetCode] Word Ladder 词语阶梯

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

  3. LeetCode:Word Ladder I II

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

  4. [leetcode]Word Ladder II @ Python

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

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

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

  6. LeetCode Word Ladder 找单词变换梯

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

  7. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  8. LeetCode: Word Ladder II 解题报告

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

  9. LeetCode: Word Ladder II [127]

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

随机推荐

  1. php-5.6.26源代码 - hash存储结构 - 添加

    添加 , (void *)module, sizeof(zend_module_entry), (void**)&module_ptr){ // zend_hash_add 定义在文件“php ...

  2. php+高德地图webapi 高德jsapi 实现 当前位置与目标位置距离 并按照距离排序(坐标逆转换)

    <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak='自己 ...

  3. NAND Flash结构及驱动函数

    目标:以NAND Flash K9F2G08U0M为例介绍其结构及其驱动程序的书写 1. 结构 由芯片手册中的图可知:K9F2G08U0M大小为2112Mbits(即 256MB = 2Gb ) 共有 ...

  4. 网站title标题被改并被百度网址安全中心提醒的解决办法

    国庆假日期间我们Sine安全接到众多网站站长求助网站标题被改导致在百度搜索中百度安全中心提醒被拦截,导致网站正常用户无法浏览网站被跳转到一些菠菜du博网站,而且很明显的一个特征就是在百度中搜索关键词的 ...

  5. Table被web编程弃用的原因

    Table要比其它html标记占更多的字节. (延迟下载时间,占用服务器更多的流量资源.)Tablle会阻挡浏览器渲染引擎的渲染顺序. (会延迟页面的生成速度,让用户等待更久的时间.)Table里显示 ...

  6. 复位自动ID的问题有兩種方法

    复位自动ID的问题 有兩種方法:      方法1:      truncate   table   你的表名   --這樣不但將數據刪除,而且可以重新置位identity屬性的字段.         ...

  7. office 总结

    wps word中双击格式刷即可开启永久格式刷

  8. kudu是什么

    Apache Kudu Overview 建议配合[Apache Kudo]审阅本文(http://kudu.apache.org/overview.html) 数据模式 Kudo是一个列式存储的用于 ...

  9. [Python 3.X]python练习笔记[2]-----用python实现七段数码管显示年月日

    #SevenDigitsDrawV2.py import turtle import time def drawGap(i):#绘制数码管间隔 turtle.penup() turtle.fd(i) ...

  10. 【Consul】关于健康检查的一点思考

    健康检查是Consul提供的一项主要功能,其配置格式如下: { "check": { "id": "redis", "name&q ...