Word Ladder

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.

最短搜索路径,所以是广度优先搜索(BFS)。

有几个问题需要注意:

1、怎样判断是否为邻居节点?

按照定义,存在一个字母差异的单词为邻居,因此采用逐位替换字母并查找字典的方法寻找邻居。

(逐个比对字典里单词也可以做,但是在这题的情况下,时间复杂度会变高,容易TLE)

2、怎样记录路径长度?

对队列中的每个单词记录路径长度。从start进入队列记作1.

长度为i的字母的邻居,如果没有访问过,则路径长度为i+1

 class Solution {
public:
int ladderLength(string start, string end, unordered_set<string> &dict) {
if(match(start,end))
return ;
queue<string> q;
map<string,int> m;
q.push(start);
m[start]=;
while(!q.empty()){
string tmp=q.front();
q.pop();
for(int i=;i<tmp.length();i++){
for(char c='a';c<='z';c++){
if(c==tmp[i])
continue;
string next=tmp;
next[i]=c;
if(next==end){
return m[tmp]+;
}
if(dict.find(next)!=dict.end()&&m.find(next)==m.end()){
q.push(next);
m[next]=m[tmp]+;
}
}
}
}
return ;
}
bool match(string src,string dst){
if(src.length()!=dst.length()){
return false;
}
int count=;
for(int i=;i<src.length();i++){
if(src[i]!=dst[i])
count++;
}
return count==?true:false;
}
};

Word Ladder(找出start——end的最短长度)——bfs的更多相关文章

  1. LeetCode Word Ladder 找单词变换梯

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

  2. LeetCode127:Word Ladder II

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

  3. 给出两个单词(start和end)与一个字典,找出从start到end的最短转换序列

    问题 给出两个单词(start和end)与一个字典,找出从start到end的最短转换序列.规则如下: 一次只能改变一个字母 中间单词必须在字典里存在 例如: 给出 start = "hit ...

  4. 给一个非常长的字符串str 另一个字符集比方{a,b,c} 找出str 里包括{a,b,c}的最短子串。要求O(n)

    给一个非常长的字符串str 另一个字符集比方{a,b,c} 找出str 里包括{a,b,c}的最短子串.要求O(n). 比方,字符集是a,b,c,字符串是abdcaabcx,则最短子串为abc. 设置 ...

  5. 找出此产品描述中包含N个关键字的长度最短的子串

    阿里巴巴笔试题:给定一段产品的英文描述,包含M个英文字母,每个英文单词以空格分隔,无其他标点符号:再给定N个英文关键词,请说明思路并变成实现方法. String extractSummary(Stri ...

  6. 输入n个字符串,找出最长最短字符串(若有个数相同的,都打印出来)

    首先,要求找到最长最短字符串,我们应该用数组将其存起来,输入的个数是不固定的,我们就可以用Scanner获取要输入的个数,最终找到的个数也不固定,我们可以封装两个方法,并且返回值类型为数组. 我遇到的 ...

  7. FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)

    题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...

  8. 【Word Ladder II】cpp

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

  9. Word Ladder系列

    1.Word Ladder 问题描述: 给两个word(beginWord和endWord)和一个字典word list,找出从beginWord到endWord之间的长度最长的一个序列,条件: 1. ...

随机推荐

  1. df和du显示的磁盘空间使用情况不一致问题

    背景介绍: dba同事删除了mysql /datao目录下的文件,通过du –sh查看空间使用700G,df -h查看空间使用1T,没有重启mysql服务. 另一个表现出du与df命令不同之处的例子如 ...

  2. Django模板导入和替换、以及对数据库的增加、查看

    静态文件引入的3中方式:例如对html模板里面对css样式的引入 STATIC_URL = '/static666/'STATICFILES_DIR=[ os.path.join(BASE_DIR,' ...

  3. [python篇][其他] python博客学习汇总

    http://blog.csdn.net/zhangxinrun/article/details/8141913

  4. redis安装、配置和启动

    一.运行环境 1.vmware虚拟机上的centos7系统,安装步骤略,网上搜搜就有,连接工具:secureCRT 2.新安装的linux,是没有wget命令,所以先执行这个命令安装下:yum -y ...

  5. oracle中xhost报错

    一.命令找不到 xhost:command not found yum whatprovides "*/xhost" Loaded plugins: product-id, sec ...

  6. openstack是什么?能干什么?

    openstack是什么?能干什么?涉及的初衷是什么?由什么来组成?刚接触openstack,说openstack不是一个软件,而是由多个组件进行组合,这是一个更深层次的理解,当我们看到dashboa ...

  7. java面试题之volatile的工作原理

    volatile的特性: volatile可见性:对一个volatile的读,总可以看到对这个变量最终的写: volatile原子性:volatile对单个读/写具有原子性(32位Long.Doubl ...

  8. bzoj3997[TJOI2015]组合数学(求最长反链的dp)

    组合数学 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一块财宝,至少走 ...

  9. maven项目打包 编码gbk的不可映射字符

    中文系统默认gbk编码格式,你的代码是utf8格式的.所以报错 <build> <plugins> <plugin> <groupId>org.apac ...

  10. 在SpringBoot中对SpringSecurity的基本使用

    参考文献: Spring Security Architecture What is authentication in Spring Security? Spring Security是一个能够为基 ...