Word Ladder

Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:

  1. Only one letter can be changed at a time
  2. 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.

 

 1 public int ladderLength(String start, String end, HashSet<String> dict) {
 2     // Start typing your Java solution below
 3     // DO NOT write main() function
 4     int len = start.length();
 5     int count = 0;
 6     HashSet<String> used = new HashSet<String>(); 
 7     if(len != end.length()) return -1;
 8     if(len == 0) return 0;
 9     Queue<String> queue = new LinkedList<String>();
10     queue.add(start);
11     int pre = 1;
12     int next = 0;
13     while(!queue.isEmpty()){
14         String cur = queue.poll();
15         used.add(cur);
16         for(int i = 0; i < len; i ++){
17             for(char c = 'a'; c < 'z' + 1; c ++){
18                 if(c == cur.charAt(i)) continue;
19                 char[] ts = cur.toCharArray();
20                 ts[i] = c;
21                 String test = String.valueOf(ts);
22                 if(dict.contains(test)){
23                     if(!used.contains(test)){
24                         if(pre != 0){
25                             next ++;
26                         }
27                         used.add(test);
28                         queue.add(test);
29                     }
30                     if(test.equals(end)){
31                         return count + 2;
32                     }
33                 }
34             }
35         }
36         pre --;
37         if(pre == 0){
38             pre = next;
39             next = 0;
40             count ++;
41         }
42     }
43     return 0;
44 }

 使用BFS的方法进行计算。对于每个string改变其一位的char。 然后判断他是不是在dict里。 同时维护一张表,保存已经访问过的string。将在dict里 且没有被访问过的string 入队列。

同时为了保存已经展开的层数,使用两个指针, 一个指向当前正在被扩展的层,pre。 一个指向队列尾next(即下一层的最后一个元素)。 没扩展当前层的一个元素 就把pre - 1。当pre== 0时, 说明当前层已经访问完了,然后将next赋值给pre, 并将next 置0。 

 

第二遍: 这一题其实就是遍历tree。

 1 public class Solution {
 2     public int ladderLength(String start, String end, HashSet<String> dict) {
 3     // Start typing your Java solution below
 4     // DO NOT write main() function
 5         int len = start.length();
 6         int count = 0;
 7         HashSet<String> used = new HashSet<String>(); 
 8         if(len != end.length() || len == 0) return 0;
 9         LinkedList<String> queue = new LinkedList<String>();
10         queue.add(start);
11         queue.add("-1");
12         while(queue.size() != 1){
13             String cur = queue.poll();
14             if(cur.equals("-1")){ 
15                 count ++;
16                 queue.add("-1");
17             }else{
18                 used.add(cur);
19                 for(int i = 0; i < len; i ++)
20                     for(char c = 'a'; c < 'z' + 1; c ++){
21                         if(c == cur.charAt(i)) continue;
22                         char[] ts = cur.toCharArray();
23                         ts[i] = c;
24                         String test = String.valueOf(ts);
25                         if(dict.contains(test) && !used.contains(test)){
26                             used.add(test);
27                             queue.add(test);
28                         }
29                         if(test.equals(end)) return count + 2;
30                 }
31             }
32         }
33         return 0;
34     }
35 }

 

posted on 2013-04-17 07:00  Step-BY-Step  阅读(170)  评论(0编辑  收藏  举报

导航