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.

题目大意:给定两个等长的单词,各一个字典,每次只能变换一个字符得到中间词,此中间词必须在字典里存在,求从一个单词变换到另一个单词最短需要多少步。

解题思路:说实话这题一开始觉得不知道从哪儿下手,后来看了下tag是BFS,才有了思路,从字典里寻找与起始单词距离为1的所有单词,加入bfs队列,然后当队列非空,取出队列中的单词,查找在字典里的所有与之距离为1的单词,直到找到结束单词或者全部遍历完没有合法解,返回0;

Talk is cheap>>

    public int ladderLength(String start, String end, Set<String> dict) {
if (transInOne(start, end)) {
return 2;
}
Queue<String> queue = new ArrayDeque<>();
queue.add(start);
int length = 1;
while (!queue.isEmpty()) {
length++;
int size = queue.size();
for (int i = 0; i < size; i++) {
String toSearch = queue.poll();
if (transInOne(toSearch, end)) {
return length;
}
for (Iterator<String> iterator = dict.iterator(); iterator.hasNext(); ) {
String next = iterator.next();
if (transInOne(toSearch, next)) {
queue.offer(next);
iterator.remove();
}
}
}
}
return 0;
} private boolean transInOne(String start, String end) {
int i = 0;
int res = 0;
while (i < start.length()) {
if (start.charAt(i) != end.charAt(i)) {
res++;
if (res > 1)
return false;
}
i++;
}
return true;
}

Word Ladder——LeetCode的更多相关文章

  1. Word Ladder - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Word Ladder - LeetCode 注意点 每一个变化的字母都要在wordList中 解法 解法一:bfs.类似走迷宫,有26个方向(即26个字 ...

  2. Word Ladder leetcode java

    题目: Given two words (start and end), and a dictionary, find the length of shortest transformation se ...

  3. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  4. [LeetCode] Word Ladder II 词语阶梯之二

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

  5. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  6. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  7. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  8. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  9. [Leetcode Week5]Word Ladder

    Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...

随机推荐

  1. hdu 1180 诡异的楼梯 (bfs)

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Sub ...

  2. npm scripts 助力前端开发,实时刷新浏览器

    用browser-sync或者lite-server来监控文件的改变,当文件改变时,就自动刷新浏览器. 用node-sass来实时编译scss文件. 用parallelshell来异步执行npm sc ...

  3. Mysql数据库一个小程序实现自动创建分表。

    每当跨月的时候也是系统出问题最多的时候,没有表和字段缺失是两个最常见的错误. 为了解决这个问题,研究了一下mysql的 information_schema 表: information_schema ...

  4. ASP.NET-FineUI开发实践-16(二)

    实现那还差点,在事件参数里我传了一个boolall选中状态参数,这个参数由前台给的,RowSelect 传的是index 行号,就是改这,通过$符号来分开的, if (commandArgs.Leng ...

  5. javascript 获取用户光标,插入文本

    图1 如图1所示,点击[函数名称],将函数名称添加到表达式内容框中,点击参数名称,将参数名称index1作为方法的参数添加到表达式内容中的表达式中. 该功能实现主要是采用了javascript获取鼠标 ...

  6. Android Marquee

    android:singleLine="true" android:marqueeRepeatLimit="marquee_forever" android:e ...

  7. (转载)用css来实现十字的布局

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 关于InstallShield Projects[转]

    关于   InstallShield   Projects:         InstallShield   可以创建三种类型的项目(Project)     1.InstallScript   Pr ...

  9. CentOS安装rar、unrar解压缩软件的方法

    闲话不说,centos上如何安装rar.unrar在线解压缩软件呢?如果您的centos是32位的,执行如下命令: wget http://www.rarsoft.com/rar/rarlinux-3 ...

  10. IOS快速开发之常量定义

    ---恢复内容开始--- 在IOS开发中,有一些方法常常需要用的,但是有很长的方法名,这造成了代码长,写起来累,我们可以通过宏定义了解决这些问题 比如说在代码布局的时候会遇上这样的问题,我们要获取上面 ...