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. http状态码(status_codes)

    首先:1XX 接受的请求正在处理,2XX请求正常处理完毕,3XX需要进行附加操作以完成请求(重定向?),4XX服务器无法处理请求(也就是客户端请求错误),5XX服务器处理请求出错. 当然不仅仅是一张图 ...

  2. thinkphp phpmailer邮箱验证

    thinkphp 关于phpmailer的邮箱验证 一  . 登陆自己的邮箱,例如:qq邮箱.登陆qq邮箱在账户设置中开启smtp服务: 之后回发送一个授权码 , 这个授权码先保存下来,这个授权码在后 ...

  3. Oracle数据库之 PL SQL 学习笔记

    1.定义基本变量: 2.引用型的变量: set serveroutput on   declare pename emp.ename%type; psal emp.sal%type;   begin ...

  4. 学习新框架laravel 5.6 (第二天)-DB,控制器及模型使用

    DB类使用,控制器使用及模型使用 链接数据库: /config/database.php /.env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=330 ...

  5. hive 学习系列四(用户自定义函数)

    如果入参是简单的数据类型,直接继承UDF,实现一个或者多个evaluate 方法. 具体流程如下: 1,实现大写字符转换成小写字符的UDF package com.example.hive.udf; ...

  6. 动态规划(DP)算法

    参考https://blog.csdn.net/libosbo/article/details/80038549 动态规划是求解决策过程最优化的数学方法.利用各个阶段之间的关系,逐个求解,最终求得全局 ...

  7. ABAP CDS - 字符串函数

    下表显示了ABAP CDS中CDS视图中字符串的潜在SQL函数,以及对参数的要求.函数的含义可以在字符串的SQL函数下找到. 函数 参数类型 返回类型 CONCAT(arg1, arg2) See b ...

  8. linux io 学习笔记(01)---锁,信号量

    1.采用信号量访问:当有段临界代码,需要保证排他的访问一个资源. 2.sudo  dmesg -c 消除dmesg缓冲 3.互斥锁:代表的是一种锁资源,互斥锁的工作原理是:保证对共享资源操作的原子性 ...

  9. PHP.43-TP框架商城应用实例-后台18-商品属性3-库存量管理

    库存量管理 思想:为商品的每个多选属性设置库存量!!要把多选属性排列组合分别指定库存量!! 效果如下:[由商品已经添加的属性决定] 1.建表goods_number{goods_id,goods_nu ...

  10. Java中的IO流体系

    Java为我们提供了多种多样的IO流,我们可以根据不同的功能及性能要求挑选合适的IO流,如图10-7所示,为Java中IO流类的体系. 注:这里只列出常用的类,详情可以参考JDK API文档.粗体标注 ...