Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) 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"]

Return

  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

Note:
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
     1 class Node {
     2     int no;
     3     String val;
     4     LinkedList<Node> prev;    
     5     
     6     Node(int no, String v) {
     7         this.no = no;
     8         this.val = v;
     9     }
    10     
    11     void addPrev(Node pNode) {
    12         if (prev == null) {
    13             prev = new LinkedList<Node>();
    14         }
    15         prev.add(pNode);
    16     }
    17 }
    18 
    19 public class Solution {    
    20     ArrayList<ArrayList<String>> answer;
    21     
    22     public void findPath(Node node, ArrayList<String> cur, String start) {
    23         if (node.val.equals(start)) {
    24             answer.add(cur);
    25             return;
    26         }
    27         ArrayList<String> temp;
    28         for (Node n : node.prev) {
    29             temp = new ArrayList<String>(cur);
    30             temp.add(0, n.val);
    31             findPath(n, temp, start);
    32         }
    33     }
    34     
    35     public ArrayList<ArrayList<String>> findLadders(String start, String end, HashSet<String> dict) {
    36         // Start typing your Java solution below
    37         // DO NOT write main() function
    38         HashMap<String, Node> map = new HashMap<String, Node>();
    39         Queue<Node> queue = new LinkedList<Node>();
    40         Node node = new Node(0, start);
    41         Node endNode = null;
    42         map.put(start, node);
    43         queue.add(node);
    44         boolean stop = false;
    45         while (queue.size() > 0 && !stop) {
    46             int count = queue.size();
    47             for (int i = 0; i < count; i++) {
    48                 node = queue.poll();
    49                 for (int j = 0; j < node.val.length(); j++) {
    50                     StringBuilder t = new StringBuilder(node.val);
    51                     for (char k = 'a'; k <= 'z'; k++) {
    52                         t.setCharAt(j, k);
    53                         if (dict.contains(t.toString())) {
    54                             Node v = map.get(t.toString());
    55                             if (v == null) {
    56                                 Node temp = new Node(node.no + 1, t.toString());
    57                                 temp.addPrev(node);
    58                                 queue.add(temp);
    59                                 map.put(t.toString(), temp);
    60                                 if (t.toString().equals(end)) {
    61                                     endNode = temp;
    62                                     stop = true;
    63                                 }
    64                             }
    65                             else {
    66                                 if (v.no == node.no + 1) {
    67                                     v.addPrev(node);
    68                                 }
    69                             }
    70                         }
    71                     }
    72                 }
    73             }
    74         }
    75         answer = new ArrayList<ArrayList<String>>();
    76         if (endNode != null) {
    77             findPath(endNode, new ArrayList<String>(Arrays.asList(end)), start);
    78         }
    79         return answer;
    80     }
    81 }
    View Code

     

posted @ 2014-02-24 05:01  krunning  阅读(226)  评论(0编辑  收藏  举报