leetcode — word-ladder
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的更多相关文章
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- [leetcode]Word Ladder @ Python
原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...
- Leetcode Word Ladder
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
随机推荐
- Linux查看端口号是否使用
netstat -lntup|grep 80 如果令命无法执行 yum install net-tools
- python可视化库 Matplotlib 00 画制简单图像
1.下载方式:直接下载Andaconda,简单快捷,减少准备环境的时间 2.图像 3.代码:可直接运行(有详细注释) # -*- encoding:utf-8 -*- # Copyright (c) ...
- 更换MariaDB数据库
https://downloads.mariadb.org/mariadb/repositories/#mirror=neusoft&distro=Ubuntu&distro_rele ...
- docker用法记录
下载docker镜像 docker pull ubuntu 查看所有docker镜像 docker images 运行docker镜像且进入shell docker run -it ubuntu ba ...
- 用python写一个非常简单的QQ轰炸机
闲的没事,就想写一个QQ轰炸机,按照我最初的想法,这程序要根据我输入的QQ号进行轰炸,网上搜了一下,发现网上的案列略复杂,就想着自己写一个算了.. 思路:所谓轰炸机,就是给某个人发很多信息,一直刷屏, ...
- linux查看分区是否开启acl权限
1.为什么需要ACL权限 ACL的全称是 Access Control List (访问控制列表) .对于文件或者目录,都有相应的操作权限 r(read 读),w(write 写),x(execute ...
- 纯javascript实现可拖住/大小的div
好久没写了,不得不说人懒了好多.. 也不打算实现什么太厉害的功能,因为不喜欢网上那些一大堆代码的,看的头晕,于是自己写了一个 旨在越简单越好(当然也走点形式- -其实是自己菜),所以一些宽度和高度都写 ...
- iOS UILabel 文字 置顶/置底 实现
iOS UILabel控件默认文字位置是居中的,如图所示: 但是我们经常碰到这样的需求,希望文字向上置顶,或者向下置底,但是很遗憾,iOS API中并没有提供相应的属性和方法,需要我们手动设置. 利用 ...
- php调用c/c++时 passthru()被禁用问题
passthru被禁用,需要编辑php.ini文件 disable_functions = scandir,passthru,exec,system,chroot,chgrp,chown,shell_ ...
- BOM与DOM操作
BOM: OM-JavaScript是运行在浏览器中的,所以提供了一系列对象用于和浏览器窗口进行交互,这些对象主要包括window.document.location.navigator和screen ...