Word Ladder(找出start——end的最短长度)——bfs
Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
- Only one letter can be changed at a time
- 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的更多相关文章
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- LeetCode127:Word Ladder II
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- 给出两个单词(start和end)与一个字典,找出从start到end的最短转换序列
问题 给出两个单词(start和end)与一个字典,找出从start到end的最短转换序列.规则如下: 一次只能改变一个字母 中间单词必须在字典里存在 例如: 给出 start = "hit ...
- 给一个非常长的字符串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. 设置 ...
- 找出此产品描述中包含N个关键字的长度最短的子串
阿里巴巴笔试题:给定一段产品的英文描述,包含M个英文字母,每个英文单词以空格分隔,无其他标点符号:再给定N个英文关键词,请说明思路并变成实现方法. String extractSummary(Stri ...
- 输入n个字符串,找出最长最短字符串(若有个数相同的,都打印出来)
首先,要求找到最长最短字符串,我们应该用数组将其存起来,输入的个数是不固定的,我们就可以用Scanner获取要输入的个数,最终找到的个数也不固定,我们可以封装两个方法,并且返回值类型为数组. 我遇到的 ...
- FCC JS基础算法题(3):Find the Longest Word in a String (找出最长单词)
题目描述: 在句子中找出最长的单词,并返回它的长度.函数的返回值应该是一个数字. 基本思路,将字符串转换成数组,然后得出数组中单个元素的长度,对长度进行排序,返回最大的一个 代码: function ...
- 【Word Ladder II】cpp
题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- Word Ladder系列
1.Word Ladder 问题描述: 给两个word(beginWord和endWord)和一个字典word list,找出从beginWord到endWord之间的长度最长的一个序列,条件: 1. ...
随机推荐
- selenium2.53用45以下的火狐别太高
selenium2.53用45以下的火狐别太高在高的火狐需要selenium3
- jade和ejs两者的特点
jade特点 1超强的可读性 2灵活易用的缩进 3块扩展 4代码默认进过编码处理,以增强安全性 5编译及运行时的上下文错误报告 6命令行编译支持 7html5模式(使用 !!!5文档类型) 8可选的内 ...
- docker (centOS 7) 使用笔记4 - etcd服务
本次测试的系统包含centos 7.2 64 bit,centos 7.3 64 bit 1. 安装 yum -y install etcd 2. 配置 此处一共准备了3台机器(10.10.10.10 ...
- 【hihocoder】欧拉路径 并查集判连通
#include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...
- 关于postman使用上发现的一点问题
之前后台用的java,一直用的postman测试接口数据,之前不管是get.post.delete.put请求都是在param传递的数据,java下面是没问题可以测试的.但是今天自己写Node发现po ...
- foj 2144 三位几何+区间覆盖
题目大意:一个人站在三维坐标系下的原点处用炮打蚊子,给出n个蚊子的起始坐标跟单位时间匀速移动的方向向量,距离他R以内的蚊子都可以打到,不过他也需要休息,没蚊子的时候也可以休息下.求他要起来多少次打蚊子 ...
- hdu 6021 MG loves string
MG loves string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others ...
- vue2 vuex 简单入门基础
1.vuex中文文档 https://vuex.vuejs.org/zh-cn/api.html 2.我理解vuex 应用观察者模式 设置了全局的状态 state 状态变化即通知更新全局state 3 ...
- 23深入理解C指针之---数组的基础
数组是c语言内置的数据结构,数组有一维数组.二维数组和多维数组.使用数组传递数组时,必须传入数组的大小. 一.数组的特征:阐明数组的特点 1.数组要素: 1).数组类型:数组中存储的数据的类型: 2) ...
- 关于Redux到底是个什么鬼
原文链接:https://zhuanlan.zhihu.com/p/20641377 我们故事的主人公,小明. 小明大学刚毕业,摆脱了宿舍的集体生活,自己在外面租了个一室一厅的小公寓住. 这是客厅的平 ...