|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 |
| - |
4 | 3 | import java.util.ArrayDeque;
|
5 | 4 | import java.util.ArrayList;
|
6 | 5 | import java.util.HashMap;
|
|
11 | 10 |
|
12 | 11 | /**
|
13 | 12 | * 126. Word Ladder II
|
14 |
| - * |
15 |
| - * Given two words (beginWord and endWord), and a dictionary's word list, |
16 |
| - * find all shortest transformation sequence(s) from beginWord to endWord, such that: |
| 13 | +
|
| 14 | + Given two words (beginWord and endWord), and a dictionary's word list, |
| 15 | + find all shortest transformation sequence(s) from beginWord to endWord, such that: |
17 | 16 |
|
18 | 17 | Only one letter can be changed at a time
|
19 | 18 | Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
|
|
37 | 36 | You may assume no duplicates in the word list.
|
38 | 37 | You may assume beginWord and endWord are non-empty and are not the same.
|
39 | 38 | */
|
| 39 | + |
40 | 40 | public class _126 {
|
41 |
| - /**Reference: https://discuss.leetcode.com/topic/2857/share-two-similar-java-solution-that-accpted-by-oj*/ |
42 | 41 |
|
43 |
| - Map<String, List<String>> map; |
44 |
| - List<List<String>> results; |
| 42 | + public static class Solution1 { |
| 43 | + /** Reference: https://discuss.leetcode.com/topic/2857/share-two-similar-java-solution-that-accpted-by-oj */ |
45 | 44 |
|
46 |
| - public List<List<String>> findLadders(String start, String end, List<String> dict) { |
47 |
| - results = new ArrayList<>(); |
48 |
| - if (dict.size() == 0) { |
49 |
| - return results; |
50 |
| - } |
| 45 | + Map<String, List<String>> map; |
| 46 | + List<List<String>> results; |
51 | 47 |
|
52 |
| - int min = Integer.MAX_VALUE; |
| 48 | + public List<List<String>> findLadders(String start, String end, List<String> dict) { |
| 49 | + results = new ArrayList<>(); |
| 50 | + if (dict.size() == 0) { |
| 51 | + return results; |
| 52 | + } |
53 | 53 |
|
54 |
| - Queue<String> queue = new ArrayDeque<>(); |
55 |
| - queue.add(start); |
| 54 | + int min = Integer.MAX_VALUE; |
56 | 55 |
|
57 |
| - map = new HashMap<>(); |
| 56 | + Queue<String> queue = new ArrayDeque<>(); |
| 57 | + queue.add(start); |
58 | 58 |
|
59 |
| - Map<String, Integer> ladder = new HashMap<>(); |
60 |
| - for (String string : dict) { |
61 |
| - ladder.put(string, Integer.MAX_VALUE); |
62 |
| - } |
63 |
| - ladder.put(start, 0); |
| 59 | + map = new HashMap<>(); |
64 | 60 |
|
65 |
| - dict.add(end); |
66 |
| - //BFS: Dijisktra search |
67 |
| - while (!queue.isEmpty()) { |
| 61 | + Map<String, Integer> ladder = new HashMap<>(); |
| 62 | + for (String string : dict) { |
| 63 | + ladder.put(string, Integer.MAX_VALUE); |
| 64 | + } |
| 65 | + ladder.put(start, 0); |
68 | 66 |
|
69 |
| - String word = queue.poll(); |
| 67 | + dict.add(end); |
| 68 | + //BFS: Dijisktra search |
| 69 | + while (!queue.isEmpty()) { |
70 | 70 |
|
71 |
| - int step = ladder.get(word) + 1;//'step' indicates how many steps are needed to travel to one word. |
| 71 | + String word = queue.poll(); |
72 | 72 |
|
73 |
| - if (step > min) { |
74 |
| - break; |
75 |
| - } |
| 73 | + int step = ladder.get(word) |
| 74 | + + 1;//'step' indicates how many steps are needed to travel to one word. |
76 | 75 |
|
77 |
| - for (int i = 0; i < word.length(); i++) { |
78 |
| - StringBuilder builder = new StringBuilder(word); |
79 |
| - for (char ch = 'a'; ch <= 'z'; ch++) { |
80 |
| - builder.setCharAt(i, ch); |
81 |
| - String newWord = builder.toString(); |
82 |
| - if (ladder.containsKey(newWord)) { |
83 |
| - |
84 |
| - if (step > ladder.get(newWord)) { |
85 |
| - //Check if it is the shortest path to one word. |
86 |
| - continue; |
87 |
| - } else if (step < ladder.get(newWord)) { |
88 |
| - queue.add(newWord); |
89 |
| - ladder.put(newWord, step); |
90 |
| - } else { |
91 |
| - // It is a KEY line. If one word already appeared in one ladder, |
92 |
| - // Do not insert the same word inside the queue twice. Otherwise it gets TLE. |
93 |
| - } |
94 |
| - if (map.containsKey(newWord)) { |
95 |
| - //Build adjacent Graph |
96 |
| - map.get(newWord).add(word); |
97 |
| - } else { |
98 |
| - List<String> list = new LinkedList(); |
99 |
| - list.add(word); |
100 |
| - map.put(newWord, list); |
101 |
| - //It is possible to write three lines in one: |
102 |
| - //map.put(new_word,new LinkedList<String>(Arrays.asList(new String[]{word}))); |
103 |
| - //Which one is better? |
104 |
| - } |
| 76 | + if (step > min) { |
| 77 | + break; |
| 78 | + } |
105 | 79 |
|
106 |
| - if (newWord.equals(end)) { |
107 |
| - min = step; |
| 80 | + for (int i = 0; i < word.length(); i++) { |
| 81 | + StringBuilder builder = new StringBuilder(word); |
| 82 | + for (char ch = 'a'; ch <= 'z'; ch++) { |
| 83 | + builder.setCharAt(i, ch); |
| 84 | + String newWord = builder.toString(); |
| 85 | + if (ladder.containsKey(newWord)) { |
| 86 | + |
| 87 | + if (step > ladder.get(newWord)) { |
| 88 | + //Check if it is the shortest path to one word. |
| 89 | + continue; |
| 90 | + } else if (step < ladder.get(newWord)) { |
| 91 | + queue.add(newWord); |
| 92 | + ladder.put(newWord, step); |
| 93 | + } else { |
| 94 | + // It is a KEY line. If one word already appeared in one ladder, |
| 95 | + // Do not insert the same word inside the queue twice. Otherwise it gets TLE. |
| 96 | + } |
| 97 | + if (map.containsKey(newWord)) { |
| 98 | + //Build adjacent Graph |
| 99 | + map.get(newWord).add(word); |
| 100 | + } else { |
| 101 | + List<String> list = new LinkedList(); |
| 102 | + list.add(word); |
| 103 | + map.put(newWord, list); |
| 104 | + //It is possible to write three lines in one: |
| 105 | + //map.put(new_word,new LinkedList<String>(Arrays.asList(new String[]{word}))); |
| 106 | + //Which one is better? |
| 107 | + } |
| 108 | + |
| 109 | + if (newWord.equals(end)) { |
| 110 | + min = step; |
| 111 | + } |
108 | 112 | }
|
109 |
| - |
| 113 | + //End if dict contains new_word |
110 | 114 | }
|
111 |
| - //End if dict contains new_word |
| 115 | + //End:Iteration from 'a' to 'z' |
112 | 116 | }
|
113 |
| - //End:Iteration from 'a' to 'z' |
| 117 | + //End:Iteration from the first to the last |
114 | 118 | }
|
115 |
| - //End:Iteration from the first to the last |
116 |
| - } |
117 |
| - //End While |
118 |
| - |
119 |
| - //BackTracking |
120 |
| - LinkedList<String> result = new LinkedList<>(); |
121 |
| - backTrace(end, start, result); |
| 119 | + //End While |
122 | 120 |
|
123 |
| - return results; |
124 |
| - } |
| 121 | + //BackTracking |
| 122 | + LinkedList<String> result = new LinkedList<>(); |
| 123 | + backTrace(end, start, result); |
125 | 124 |
|
126 |
| - private void backTrace(String word, String start, List<String> list) { |
127 |
| - if (word.equals(start)) { |
128 |
| - list.add(0, start); |
129 |
| - results.add(new ArrayList<>(list)); |
130 |
| - list.remove(0); |
131 |
| - return; |
| 125 | + return results; |
132 | 126 | }
|
133 |
| - list.add(0, word); |
134 |
| - if (map.get(word) != null) { |
135 |
| - for (String s : map.get(word)) { |
136 |
| - backTrace(s, start, list); |
| 127 | + |
| 128 | + private void backTrace(String word, String start, List<String> list) { |
| 129 | + if (word.equals(start)) { |
| 130 | + list.add(0, start); |
| 131 | + results.add(new ArrayList<>(list)); |
| 132 | + list.remove(0); |
| 133 | + return; |
| 134 | + } |
| 135 | + list.add(0, word); |
| 136 | + if (map.get(word) != null) { |
| 137 | + for (String s : map.get(word)) { |
| 138 | + backTrace(s, start, list); |
| 139 | + } |
137 | 140 | }
|
| 141 | + list.remove(0); |
138 | 142 | }
|
139 |
| - list.remove(0); |
140 | 143 | }
|
141 |
| - |
142 | 144 | }
|
0 commit comments