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. AT与ATX电源 - 1 系统状态

    ATX与AT电源比较 ATX电源普遍应用在PC中,它有两套电源,一个是正常操作使用:12V,5V,3.3V和-12V,还有一个独立的5V待机电源,所谓的待机电源就是其ON的充要条件是AC输入存在,而正 ...

  2. centos查看apache用的是哪个httpd.conf

    httpd -V得到类似如下结果: -D HTTPD_ROOT="/etc/httpd" -D SERVER_CONFIG_FILE="conf/httpd.conf&q ...

  3. 把.zip文件转化为.tar.gz文件

    工作中正好用到上传tar.gz文件,没有现成的转换工具,就写了方法转换 #encoding: utf-8import osimport tarfileimport zipfileimport osim ...

  4. Round #2

    题源:来自hzwer整理的题 2014-5-10 NOIP模拟赛 by coolyangzc Problem 1 机器人(robot.cpp/c/pas) [题目描述] 早苗入手了最新的Gundam模 ...

  5. PHP 反射的简单使用

    反射机制简介 之前已经介绍过Java反射机制的简单使用,所有的反射机制的思想作用等都是类似的,下面就一起来了解一下PHP反射机制. 个人理解:反射机制就是可以利用类名或者一个类的对象来获取关于这个类的 ...

  6. layui select使用问题

    1.需要引用form模板 layui.use(['form'], function () { var form = layui.form; }); 2.html代码 <div class=&qu ...

  7. linux操作命令之搜索命令

    1.文件搜索命令:locate 文件名 在后台数据库中按照文件名搜索,搜素速度更快 /var/lib/mlocate:#locate命令所搜索的后台数据库 updatedb:更新数据库 updated ...

  8. python 模型 ORM简介

    Django之ORM (Object Relational Mapping(ORM)一.ORM介绍1.ORM概念 对象关系映射模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术.2.OR ...

  9. ABP入门系列(3)——领域层定义仓储并实现

    ABP入门系列目录--学习Abp框架之实操演练 一.先来介绍下仓储 仓储(Repository): 仓储用来操作数据库进行数据存取.仓储接口在领域层定义,而仓储的实现类应该写在基础设施层. 在ABP中 ...

  10. ubuntu系统界面改变

    主题:https://gitzab.com/Anduin/GNOME-OSX-II-Theme.git图标:https://github.com/keeferrourke/la-capitaine-i ...