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. HTML 简介

    什么是HTML HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言: Hyper Text Markup Language HTML 不是一种编程语言,而是一种标记语言 标记语言是一 ...

  2. XML模块

    XML 例子: # -*- encoding:utf-8 -*- import requests from xml.etree import ElementTree as ET f = request ...

  3. 在IE6、IE7中实现块元素的inline-block效果

    在IE6.IE7中实现块元素的inline-block效果有以下两种方法: 1先使用display:inline-block属性触发layout,然后再定义display:inline让块元素呈现内联 ...

  4. 再说linux中的rm mv 遍历执行多个文件的操作: find + xagrs

    参考文章: http://cfqtyaogang.blog.163.com/blog/static/218051022011812111342203/, 这篇文章讲得很全面很详细... 包括不好理解的 ...

  5. c++模板

    1.从 python 说起 def add(a, b): return a + b; print add(3.1, 5.1); #8.2 print add("abc", &quo ...

  6. HTML5+CSS3+jquery实现简单的音乐播放器

    ...最近天热的,感觉就像煎饼...然后别人在把妹子的时候,只有偶们这帮苦逼的程序员在那边撸代码...我日哦! 然后今天晒的是偶早年写的一个播放器...看上去是不是很有感觉的样子!一番宝物,Lisa唱 ...

  7. 阿里云9折推荐码:0LGVW2

    阿里云9折推荐码:0LGVW2,第一次购买云服务器或云数据库可享受原价9折优惠.

  8. Todd's Matlab讲义第4讲:控制误差和条件语句

    误差和残量 数值求解方程\(f(x)=0\)的根,有多种方法测算结果的近似程度.最直接的方法是计算误差.第\(n\)步迭代结果与真值\(x^\*\)的差即为第\(n\)步迭代的误差: \begin{e ...

  9. 被拒原因——You have selected the Kids Category for your app, but it does not include the required privacy policy. Please update your app metadata to include a privacy policy URL and ensure that the URL yo

    对于一些孩子类的应用,必须加上隐私政策网址(URL),直接截个图吧! 就是你上架的时候,填写应用信息,里面有一个隐私政策网址(URL),望后者不掉坑里了!!!

  10. php运行出现Call to undefined function curl_init()的解决方法

    解决方法如下: 1.在php.ini中找到extension=php_curl.dll,去掉前面的分号;,然后将php.ini拷贝到c:\windows. 2.重启IIS服务,或回收应用程序池即可.