Question

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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.

Solution 1 -- BFS

 class WordNode{
String word;
int numSteps; public WordNode(String word, int numSteps){
this.word = word;
this.numSteps = numSteps;
}
} public class Solution {
public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
Queue<WordNode> queue = new LinkedList<WordNode>();
queue.add(new WordNode(beginWord, 1)); wordList.add(endWord); // 由于这里纪录了每个词的最小步数,所以不用两个list
while (queue.size() > 0) {
WordNode topNode = queue.remove();
String current = topNode.word; if (current.equals(endWord))
return topNode.numSteps; char[] arr = current.toCharArray();
// 穷举法
for (int i = 0; i < arr.length; i++) {
for (char c = 'a'; c <= 'z'; c++) {
char tmp = arr[i];
if (arr[i] != c)
arr[i] = c; String newWord = new String(arr);
if (wordList.contains(newWord)) {
queue.add(new WordNode(newWord, topNode.numSteps + 1));
wordList.remove(newWord);
}
arr[i] = tmp;
}
}
}
return 0;
}
}

Solution 2 -- BFS & Adjacency List

Basic idea is: 1. construct adjacency list 2. BFS

Constructing adjacency list uses O(n2) time, and BFS is O(n). Total time complexity is O(n2), when testing in leetcode, it reports "time limit exceeded".

 public class Solution {
public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
if (wordList == null)
return -1;
int step = 1;
if (beginWord.equals(endWord))
return 1;
// Construct adjacency lists for words
// Do not forget start word and end word
wordList.add(beginWord);
wordList.add(endWord);
Map<String, List<String>> adjacencyList = new HashMap<String, List<String>>();
Map<String, Boolean> visited = new HashMap<String, Boolean>();
int length = wordList.size();
String[] wordList2 = new String[length];
int i = 0;
for (String current : wordList) {
wordList2[i] = current;
i++;
}
// Initialization
for (i = 0; i < length; i++) {
adjacencyList.put(wordList2[i], new ArrayList<String>());
visited.put(wordList2[i], false);
} for (i = 0; i < length; i++) {
String current = wordList2[i];
// Construct adjacency list for each element
for (int j = i + 1; j < length; j++) {
String next = wordList2[j];
if (isAdjacent(current, next)) {
List<String> list1 = adjacencyList.get(current);
list1.add(next);
adjacencyList.put(current, list1);
List<String> list2 = adjacencyList.get(next);
list2.add(current);
adjacencyList.put(next, list2);
}
}
} // BFS
List<String> current = new ArrayList<String>();
List<String> next;
current.add(beginWord);
visited.put(beginWord, true);
while (current.size() > 0) {
step++;
next = new ArrayList<String>();
for (String currentString : current) {
List<String> neighbors = adjacencyList.get(currentString);
if (neighbors == null)
continue;
for (String neighbor : neighbors) {
if (neighbor.equals(endWord))
return step;
if (!visited.get(neighbor)) {
next.add(neighbor);
visited.put(neighbor, true);
}
}
}
current = next;
}
return -1; } private boolean isAdjacent(String current, String next) {
if (current.length() != next.length())
return false;
int length = current.length();
int diff = 0;
for (int i = 0; i < length; i++) {
char a = current.charAt(i);
char b = next.charAt(i);
if (a != b)
diff++;
}
if (diff == 1)
return true;
return false;
}
}

Python version

 from collections import deque
from collections import defaultdict class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
if endWord not in wordList:
return 0
queue = deque()
queue.append((beginWord, 1))
visited = set([beginWord])
adjacency_map = defaultdict(list)
L = len(beginWord)
# Pre-process
for word in wordList:
for i in range(L):
adjacency_map[word[:i] + "*" + word[i+1:]].append(word)
# BFS
while queue:
curr_word, curr_steps = queue.popleft()
if curr_word == endWord:
return curr_steps
# List all possibilities
for i in range(L):
key = curr_word[:i] + "*" + curr_word[i+1:]
if key in adjacency_map:
neighbors = adjacency_map[key]
for neighbor in neighbors:
if neighbor == endWord:
return curr_steps + 1
if neighbor not in visited:
queue.append((neighbor, curr_steps + 1))
visited.add(neighbor)
adjacency_map[key] = {}
return 0

Word Ladder 解答的更多相关文章

  1. Word Ladder II 解答

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

  2. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

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

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

  4. LeetCode:Word Ladder I II

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

  5. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  6. 【leetcode】Word Ladder II

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

  7. 18. Word Ladder && Word Ladder II

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  8. [Leetcode][JAVA] Word Ladder II

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

  9. LeetCode127:Word Ladder II

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

随机推荐

  1. hihoCoder 1092 : Have Lunch Together

    题目大意:小hi和小ho去咖啡厅喝咖啡,咖啡厅可以看作是n * m的矩阵,每个点要么为空,要么被人.障碍物.椅子所占据,小hi和小ho想要找两个相邻的椅子.起初两个人都在同一个点,求两人到达满足要求的 ...

  2. 链表的基本操作(Basic Operations on a Linked List)

    链表可以进行如下操作: 创建新链表 增加新元素 遍历链表 打印链表 下面定义了对应以上操作的基本函数. 创建新链表 新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表.由 ...

  3. mysql的日志

    是否启用了日志mysql>show variables like ‘log_bin’; 怎样知道当前的日志mysql> show master status; 看二进制日志文件用mysql ...

  4. 提高xshell使用效率

    1.快速命令集. 2.鼠标复制粘贴设置. 3.配色方案. 4.esc切换到英文输入. 设置入口:

  5. RCU 机制 [转IBM]

    2005 年 7 月 01 日 本文详细地介绍了 Linux 2.6 内核中新的锁机制 RCU(Read-Copy Update) 的实现机制,使用要求与典型应用. 一.引言 众所周知,为了保护共享数 ...

  6. Cookie知识点小结

    问题是什么?有哪些技术?如何解决? 1. Cookie 1)完成回话跟踪的一种机制:采用的是在客户端保存Http状态信息的方案 2)Cookie是在浏览器访问WEB服务器的某个资源时,由WEB服务器在 ...

  7. Java遍历Map对象的四种方法

    在java中遍历Map有不少的方法.我们看一下最常用的方法及其优缺点. 既然java中的所有map都实现了Map接口,以下方法适用于任何map实现(HashMap, TreeMap, LinkedHa ...

  8. 基于google earth engine 云计算平台的全国水体变化研究

    第一个博客密码忘记了,今天才来开通第二个博客,时间已经过去两年了,三年的硕士生涯,真的是感慨良多,最有收获的一段时光,莫过于在实验室一个人敲着代码了,研三来得到中科院深圳先进院,在这里开始了新的研究生 ...

  9. 在多线程环境中使用Jedis

    Jedis是一个Java语言的Redis客户端,它为Java语言连接与操作Redis提供了简单易用的接口. Jedis不是线程安全的.故不应该在多线程环境中共用一个Jedis实例.可是.也应该避免直接 ...

  10. PHP获取中英文混合字符串长度及截取

    1.字符串长度 PHP获取中英文混合字符串长度的实现代码如下,1中文=1位,2英文=1位,可自行修改 /** * PHP获取字符串中英文混合长度 * @param $str string 字符串 *  ...