LeetCode: Word Ladder解题报告

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.

SOLUTION 1:

经典的BFS题目。
想象一下,这个变换过程是一个树,每一层是当前所有的变换结果 ,下一层又是上一层的字符串的所有的变换结果。例子:
HIT
AIT, BIT, CIT, DIT..... 

    HAT, HBT, HCT, HDT.....    HIA, HIB, HIC, HID....

HIT 可以有这么多种变换方式,而AIT, BIT本身也可以以相同的方式展开,这就形成了一个相当大的树。
HIT
AIT, BIT, CIT, DIT.....     HAT, HBT, HCT, HDT.....    HIA, HIB, HIC, HID....
 |    (BIT,CIT这些没有再展开了,画图实在不方便)
 |
AIT, BIT, CIT, DIT...     ABT, ACT, ADT....

 1 public class Solution {
 2     public int ladderLength(String start, String end, Set<String> dict) {
 3         if (start == null || end == null || dict == null) {
 4             return 0;
 5         }
 6         
 7         // Bug 1: quese is a interface not a class
 8         Queue<String> q = new LinkedList<String>();
 9         q.offer(start);
10         
11         HashSet<String> set = new HashSet<String>();
12         set.add(start);
13         
14         // Bug 3: lever start from 1;
15         int level = 1;
16         
17         while (!q.isEmpty()) {
18             int size = q.size();
19             
20             level++;
21             
22             for (int i = 0; i < size; i++) {
23                 String s = q.poll();
24                 
25                 int len = s.length();
26                 
27                 for (int j = 0; j < len; j++) {
28                     StringBuilder sb = new StringBuilder(s);
29                     for (char c = 'a'; c <= 'z'; c++) {
30                         // Bug 2: setCharAt
31                         sb.setCharAt(j, c);
32                         String tmp = sb.toString();
33                         
34                         // 按照题意,这句应该在前,因为题目并不要求end在dict中。
35                         if (tmp.equals(end)) {
36                             return level;
37                         }
38                         
39                         // Should be in the dict and not in the hashset.
40                         if (set.contains(tmp) || !dict.contains(tmp)) {
41                             continue;
42                         }
43                         
44                         set.add(tmp);
45                         q.offer(tmp);
46                     }
47                 }
48                 
49             }
50         }
51         
52         // When not found, return 0;
53         // "hot", "dog", ["hot","dog"]
54         return 0;
55     }
56 }
View Code

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/bfs/LadderLength_1218_2014.java

posted on 2014-12-18 19:07  Yu's Garden  阅读(1079)  评论(2编辑  收藏  举报

导航