Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) 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"]

Return

[
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]
解题思路:

本题应该是目前遇到的最Hard的一道了,思路先按照Java for LeetCode 127 Word Ladder的代码进行dfs找到 ladderLength 然后以 ladderLength 为步长进行DFS,这里进行DFS需要从后往前(因为disMap是从前往后建立的,从前往后的话前期肯定有无数匹配,从后往前的话,只要匹配就是要找到alist)

JAVA实现如下:

static public List<List<String>> findLadders(String start, String end,Set<String> dict) {
List<List<String>> result = new LinkedList<List<String>>();
LinkedList<String> queue = new LinkedList<String>();
HashMap<String, Integer> disMap = new HashMap<String, Integer>();
queue.add(start);
disMap.put(start, 1);
int depth = -1;
findDepth: while (!queue.isEmpty()) {
String word = queue.poll();
for (int i = 0; i < word.length(); i++) {
StringBuilder sb = new StringBuilder(word);
for (char ch = 'a'; ch <= 'z'; ch++) {
sb.setCharAt(i, ch);
String nextWord = sb.toString();
if (nextWord.equals(end)) {
depth = disMap.get(word) + 1;
break findDepth;
}
if (dict.contains(nextWord)
&& !disMap.containsKey(nextWord)) {
queue.add(nextWord);
disMap.put(nextWord, disMap.get(word) + 1);
}
}
}
}
if (depth > 0)
dfs(result, start, end, disMap, depth,new LinkedList<String>());
return result;
} static void dfs(List<List<String>> result, String start, String end,HashMap<String, Integer> disMap, int depth,List<String> alist) {
alist.add(0, end);
if (end.equals(start))
result.add(new LinkedList<String>(alist));
if (depth <= 1)
return;
String word = alist.get(0);
for (int i = 0; i < word.length(); i++) {
StringBuilder sb = new StringBuilder(word);
for (char ch = 'a'; ch <= 'z'; ch++) {
sb.setCharAt(i, ch);
String nextWord = sb.toString();
if (disMap.containsKey(nextWord)&&disMap.get(nextWord)==depth-1) {
dfs(result, start, nextWord, disMap, depth - 1,alist);
alist.remove(0);
}
}
}
}

Java for LeetCode 126 Word Ladder II 【HARD】的更多相关文章

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

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

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

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

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

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

  4. leetcode 126. Word Ladder II ----- java

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

  5. Leetcode#126 Word Ladder II

    原题地址 既然是求最短路径,可以考虑动归或广搜.这道题对字典直接进行动归是不现实的,因为字典里的单词非常多.只能选择广搜了. 思路也非常直观,从start或end开始,不断加入所有可到达的单词,直到最 ...

  6. leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)

    https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a diction ...

  7. leetcode 127. Word Ladder、126. Word Ladder II

    127. Word Ladder 这道题使用bfs来解决,每次将满足要求的变换单词加入队列中. wordSet用来记录当前词典中的单词,做一个单词变换生成一个新单词,都需要判断这个单词是否在词典中,不 ...

  8. 126. Word Ladder II(hard)

    126. Word Ladder II 题目 Given two words (beginWord and endWord), and a dictionary's word list, find a ...

  9. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

随机推荐

  1. Android Retrofit使用教程(三):Retrofit与RxJava初相逢

    上一篇文章讲述了Retrofit的基本使用,包括GET,POST等请求.今天的文章中Retrofit要与RxJava配合使用. 了解RxJava RxJava有种种好处,我不在这里一一讲述.这里我只给 ...

  2. php正则表达式取子字符串及替换

    最近在学习如何用php编写cms,想把文章中的第一个图片提取出来当做缩略图显示到前面,想到的方法就是把文章内容作为一个大字符串,然后用正则表达式找出匹配出第一次出现<img src=" ...

  3. MFC中 CString类型用fprintf 函数写到文件中乱码的解决办法

    在上一篇中记录了用fprintf函数写内容到文件中的方法,但是发现了问题:产生的文件字符串有乱码现象. 解决办法:用_ftprintf函数 另外,据说: unicode的话要用fwprintf    ...

  4. 2016.6.20 eclipse安装完毕后打开失败,显示a JDK or a JRE must be avaliable in order to run Eclispe

    下载完成后,点击eclipse.exe,跳出如下错误. 按照百度的方法,修改了一下eclispe的配置文件,不仅没解决问题,结果跳出另一个新的错误: Failed to load the JNI sh ...

  5. apache TIME_WAIT解决办法

    最近发现apache与负载均衡器的的连接数过多,而且大部分都是TIME_WAIT,调整apache2.conf后也没效果,最后百度到如下解决方案 通过调整内核参数解决 vi /etc/sysctl.c ...

  6. MQTT---HiveMQ源代码具体解释(十四)Persistence-LocalPersistence

    源博客地址:http://blog.csdn.net/pipinet123 MQTT交流群:221405150 简单介绍 HiveMQ的Persistence提供配置包含File和Memory,以解决 ...

  7. 协程(Coroutine)并不是真正的多线程(转)

    自:http://www.zhihu.com/question/23895384 说到Coroutine,我们必须提到两个更远的东西.在操作系统(os)级别,有进程(process)和线程(threa ...

  8. 关于 html 中 table 表格 tr,td 的高度和宽度

    http://wenku.baidu.com/link?url=76BZcBS3YyA1QJwE7pCPJKERPex4uSQEQ1LI5ZkwTCtunw2cBTaLI8E71dxUhFW0CH4h ...

  9. Notepad2替换记事本--映像劫持

     Image File Execution Options就是映像劫持技术,通过此种方式替换记事本,非常地绿色环保. Image File Execution Options是CreateProces ...

  10. 配置fio支持rbd測试引擎

    fio的rbd測试引擎能够非常方便的对rbd进行測试.以下示范怎样安装fio支持rbd引擎. git clone git://git.kernel.dk/fio.git $ cd fio $ ./co ...