Word Ladder

Total Accepted: 24823 Total Submissions: 135014My Submissions

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.
Hide Tags

Breadth-first Search

 
 
利用广度优先搜索,找到元素的深度即可
寻找相差一个字符的字符串时,考虑采用替换字符的方式寻找,遍历dict在dict很长时,会耗时
 
 int ladderLength(string start, string end, unordered_set<string> &dict) {
int n=start.size();
if(n<||n!=end.size())
{
return ;
}
if(start==end)
{
return ;
} int level=;
queue<string> q;
q.push(start);
//count用来记录每一个深度的元素的个数
int count=;
while()
{
start=q.front();
q.pop();
count--;
for(int i=;i<start.length();i++)
{
string ori=start;
//每次修改一个字符,看是否在字典中能找到
for(char ch='a';ch<='z';ch++)
{
if(start[i]==ch)continue; start[i]=ch;
if(start==end) return level;
//如果能找到,则用queue记录下下一层深度的元素
if(dict.find(start)!=dict.end())
{
dict.erase(start);
q.push(start);
}
start=ori;
}
} //没有下一层深度了,或者dict已经为空
if(q.empty()||dict.empty())
{
break;
} //count为0,说明该level的元素已经被遍历完了
if(count==)
{
level++;
count=q.size();
}
}
return ;
}
 

【leetcode】Word Ladder的更多相关文章

  1. 【leetcode】Word Ladder II

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

  2. 【题解】【字符串】【BFS】【Leetcode】Word Ladder

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  3. 【leetcode】Word Ladder (hard) ★

    Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...

  4. 【leetcode】Word Ladder II(hard)★ 图 回头看

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

  5. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  6. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  7. 【leetcode】Word Break II

    Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...

  8. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  9. 【leetcode】Word Search (middle)

    今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...

随机推荐

  1. iOS静态库小结--(yoowei)

    准备知识: 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.根据源代码的公开情况,库可以分为2种类型 a.开源库 公开源代码,能看到具体实现 ,比如SDWebImage.AFNetw ...

  2. CSS 改变文本选中颜色

    改变文字颜色 ::selection {    background: #f88;    text-shadow: none;    color: #000;}::-moz-selection {  ...

  3. Java读取txt文件,计算2011年9月份的通话时间

    public class test2 { /** * @param args * @throws IOException */ public static void main(String[] arg ...

  4. Yii2提交表单提示无法验证

    yii2使用gii生成的搜索视图里的表单使用的是get方式,我改为post就提示无法验证,以为是控制器默认访问是get,实际默认是get和post都可以 public function behavio ...

  5. Hibernate SQL优化技巧dynamic-insert="true" dynamic-update="true"

    最近正在拜读Hibernate之父大作<Java Persistence with Hibernate>,颇有收获.在我们熟悉的Hibernate映射文件中也大有乾坤,很多值得我注意的地方 ...

  6. mapreduce 模板

    /*** * MapReduce Module * @author nele * */ public class ModuleMapReduce extends Configured implemen ...

  7. 看懂理解 keyboard中 , navigation的设置: 切换工作区和移动窗口到不同的工作区.

    navigation中, 主要有两个方面的内容: 移动窗口到工作区 的shortcuts 切换工作区的shortcuts 首先清楚: 工作区workspace, fedora 23中 好像只有上下方向 ...

  8. 低版本IE浏览器 input元素出现叉叉的情况

    都说是IE10之上的浏览器才有这个问题,恰巧我IE10之上都没有问题,反而是低版本的浏览器出现了这个问题.作为一个凭证,我先放一张图片在这里面. 之前无意中解决过这个问题,如今复现确实是没有解决,网上 ...

  9. java 练手 谁是最好的Coder

    Problem A 谁是最好的Coder 时间限制:1000 ms  |  内存限制:65535 KB   描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所 ...

  10. 【codevs1380】没有上司的舞会

    题目描述 Ural大学有N个职员,编号为1~N.他们有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.每个职员有一个快乐指数.现在有个周年庆宴会,要求与会职员的快乐指数 ...