import java.util.*;

/**
* Source : https://oj.leetcode.com/problems/word-ladder/
*
*
* Given two words (start and end), and a dictionary, find the length of shortest
* transformation sequence 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"]
*
* 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.
*
*/
public class WordLadder { /**
*
* 转化为图的问题
* start、end、dict中各个单词看做是图中的每个节点
* 如果有一个单词能变化一个字母到另外一个单词,说明两个节点是连通的
*
* 所以就转化为求两个节点之间的最短距离,使用BFS
*
* 使用BFS注意:
* 1. 找到当前需要遍历的节点,这里是当前节点的相邻节点
* 2. 标记已经遍历过的节点,防止从重复遍历
*
* 从start开始使用BFS,遍历当前节点的相邻节点,求出当前节点的相邻节点两种办法:
* 1. 遍历字典中每个单词,如果和当前单词只差一个字母,说明是相邻的,复杂度为:w*n,w是当前单词的长度,n是字典单词数量
* 2. 针对当前单词的每个字母,找出可能得变化,每个字母可以变为26个字母中除本身外的其他字母,如果判断变化后的单词在字典中则为相邻的节点,
* 复杂度为 26*w,w为单词长度
*
* 当字典中单词数量较小的时候可以使用第一种方法,如果字典中单词数量较大则使用第二种方法
*
* 怎么标记访问过的节点?
* 已经访问过的节点不需要再次被访问,所以可以从字典中删除
*
* 这里使用第二种方法
*
* @param start
* @param end
* @param dict
* @return
*/
public int ladderLength (String start, String end, String[] dict) {
Set<String> set = new HashSet<String>(Arrays.asList(dict));
set.add(end);
Map<String, Integer> map = new HashMap<String, Integer>(); map.put(start, 1);
while (map.size() > 0) {
String cur = map.keySet().iterator().next();
Integer len = map.get(cur);
if (cur.equals(end)) {
System.out.println(len);
// return len;
}
map.remove(cur);
Set<String> neighbors = findNeighbors(cur, set);
for (String str : neighbors) {
map.put(str, len+1);
}
} return 0; } private Set<String> findNeighbors (String cur, Set<String> dict) {
Set<String> neighbors = new HashSet<String>();
for (int i = 0; i < cur.length(); i++) {
for (int j = 0; j < 26; j++) {
char ch = (char) ('a' + j);
if (cur.charAt(i) != ch) {
String candidate = "";
if (i == cur.length()-1) {
candidate = cur.substring(0, i) + ch;
} else {
candidate = cur.substring(0, i) + ch + cur.substring(i+1);
}
if (dict.contains(candidate)) {
neighbors.add(candidate);
dict.remove(candidate);
}
}
}
}
return neighbors;
} public static void main(String[] args) {
WordLadder wordLadder = new WordLadder();
String start = "hit";
String end = "cog";
String[] dict = new String[]{"hot","dot","dog","lot","log"};
System.out.println(wordLadder.ladderLength(start, end, dict) + "----5"); }
}

leetcode — word-ladder的更多相关文章

  1. LeetCode:Word Ladder I II

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

  2. [leetcode]Word Ladder II @ Python

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

  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 II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

  6. LeetCode: Word Ladder II 解题报告

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

  7. LeetCode: Word Ladder II [127]

    [题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  8. LeetCode Word Ladder 找单词变换梯

    题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...

  9. [leetcode]Word Ladder @ Python

    原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...

  10. Leetcode Word Ladder

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

随机推荐

  1. C# 神奇的Web services 请求超时问题 排查分析

    服务器上有两个接口,一个是Web Services(asmx文件)接口,一个是MVC API (普通的GET请求接口) 神奇的事情是这样的,只要我使用WebRequest请求两次,再使用Web Ser ...

  2. python3 实现对代码文件中注释的翻译

    心血来潮,昨天看见群里讨论一段图片转pdf的代码,直接拿过来发现不能用,原来是因为代码中传的文件对象不对,源码中说传递的是一个文件路径或一个文件对象.他传的是一个文件名,很是尴尬. 想了想,主要原因还 ...

  3. nginx.conf 中php-ftp配置

    location ~ .php$ { root /home/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_par ...

  4. HTML 5将给开发者带来什么?

    在新的时代里,相信网页技术会伴随HTML 5的来临进入大洗牌的局面,HTML 5旨在解决Web中的交互,媒体,本地操作等问题,一些浏览器已经尝试支持HTML 5的一些功能,而开发者们有望最终从那些We ...

  5. VB6进行GZIP解压&C#进行GZIP压缩和解压

    VB进行GZIP解压的,DLL是系统的,如果没有 [点击下载] Option Explicit 'GZIP API '----------------------------------------- ...

  6. react基础篇 整理(一)

    备注不知道为啥不能到出图片,详细知识自己百度一下就可以了,很简单的.画这个是为了更好的梳理知识,公司有个App项目,项目可控,所以尝试一下用React-native去做一下试试.

  7. linux操作命令之帮助命令

    一.man命令的帮助: man 命令名 获取指定命令的帮助 例如man ls 查看ls的帮助 man man可以看到man有8个级别的man帮助命令使用场景 1.查看命令的帮助 2.查看可被内核调用的 ...

  8. c# 钩子类

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  9. Data Center手册(2): 安全性

    有个安全性有下面几种概念: Threat:威胁 Vulnerability: 安全隐患 Attack: 攻击 有关Threat 常见的威胁有下面几种 DoS(Denial of Service拒绝服务 ...

  10. 实战深度学习OpenCV(一):canny边缘检测

    利用canny边缘检测,我们可以很好地得到哦一个图像的轮廓,下面是基于C++的,这是我们通过这段代码得到的结果: #include "pch.h" #include <ios ...