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.

乍看起来很难的一题,其实仔细分析就是图的遍历。把start,end和dict里的词都看作一个个结点,如果一个词可以合法转化为另一个词,那么视为这两个“结点”中间有一条路径。问题则变为,找到从start到end的最短路径长度。

如果采用扫描所有dict中单词,然后判断是否合法来遍历结点的方法,会超时(因为有dict中单词过多的用例)。所以只能采用别的遍历思路。

一个有效的遍历思路是获取到当前单词长度,然后对每一位都尝试替换为别的字符来遍历,即得到所以可能的合法字符串,然后判断是否等于end,或者在dict中且不在已遍历过的结点中,这样时间复杂度就是O(l)仅仅跟单词长度有关了。

对每一个结点,都用HashMap保存start到它的路径长度,新结点进入时只需要把长度加一即可。

采用广度优先遍历比较好,因为我们只需要获得最短的路径长度,而广度优先可以保证第一个到达end的路径是最短的(即到达end即可以return)。

代码如下:

     public int ladderLength(String start, String end, Set<String> dict) {
HashMap<String, Integer> hm = new HashMap<String, Integer>();
Queue<String> q = new LinkedList<String>();
q.add(start);
hm.put(start,1); while(!q.isEmpty())
{
String temp = q.poll();
int len = hm.get(temp);
for(int i=0;i<temp.length();i++) {
for(char c='a';c<='z';c++) {
if(c==temp.charAt(i))
continue;
StringBuilder sb = new StringBuilder(temp);
sb.setCharAt(i,c);
String next = sb.toString(); if(next.equals(end))
return len+1;
if(dict.contains(next) && !hm.containsKey(next)) {
q.add(next);
hm.put(next,len+1);
}
}
}
}
return 0;
}

[Leetcode][JAVA] Word Ladder的更多相关文章

  1. [Leetcode][JAVA] Word Ladder II

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

  2. Java for LeetCode 126 Word Ladder II 【HARD】

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

  3. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

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

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  5. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  6. LeetCode 127. Word Ladder 单词接龙(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shorte ...

  7. [LeetCode#128]Word Ladder II

    Problem: Given two words (start and end), and a dictionary, find all shortest transformation sequenc ...

  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. java文件保存至服务器

    import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java ...

  2. 常用vim设置

    set tabstop=4set shiftwidth=4set expandtabset hlsearchset cindent set autoindent set tabstop=4是设TAB宽 ...

  3. 29、shiro框架入门

    1.建立测试shiro框架的项目,首先建立的项目结构如下图所示 ini文件 中的内容如下图所示 pom.xml文件中的内容如下所示 <project xmlns="http://mav ...

  4. arpg网页游戏之地图(一)

    [转]http://www.cnblogs.com/BlueWoods/p/4681572.html?from=timeline&isappinstalled=1 开发arpg网页游戏一项比较 ...

  5. 排序算法 2 qsort 库函数,泛型函数

    _____谈谈排序算法 交换排序——>冒泡排序-->快速排序 选择排序——>简单选择排序——>堆排序 插入排序——>直接插入排序——>希尔排序 _____排序算法对 ...

  6. 【原】JS正则表达式里的控制符

    正则表达式易于使用而又让人费解,乍一看上去,就像是一行行的乱码,但是它的功能确实又不容小觑.今天整理正则时,纠正了自己的一个误解. 先缕一缕: 正则表达式的两种声明方式: 字面量.构造器 (RegEx ...

  7. VC++ 中简单操作MP3音乐的方法,小结

    #include <windows.h> #include <stdio.h> #include <mmsystem.h> #include <shellap ...

  8. 从零开始搭建Docker Swarm集群

    从零开始搭建Docker Swarm集群 检查节点Docker配置 1. 打开Docker配置文件(示例是centos 7)vim /etc/sysconfig/docker2. 添加-H tcp:/ ...

  9. WCF学习第一天

    WCF 设计目标 : 1:生产效率 : 统一了现有的各种分布式技术                          基于属性(Attribute)的开发                       ...

  10. [原创]PCB知识补充

    近期又要使用Altium进行PCB板的绘制,算起来从大学课上第一次接触Protel99SE到现在已经算是半个熟练工了.不过现在想来还是能回忆起第一次使用的情景,对着一幅简单的原理图使用着自动连线的功能 ...