|
| 1 | +## 126. Word Ladder II |
| 2 | + |
| 3 | +### Question: |
| 4 | +Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: |
| 5 | +* Only one letter can be changed at a time |
| 6 | +* Each transformed word must exist in the word list. Note that beginWord is not a transformed word. |
| 7 | + |
| 8 | +Note: |
| 9 | +1. Return an empty list if there is no such transformation sequence. |
| 10 | +2. All words have the same length. |
| 11 | +3. All words contain only lowercase alphabetic characters. |
| 12 | +4. You may assume no duplicates in the word list. |
| 13 | +5. You may assume beginWord and endWord are non-empty and are not the same. |
| 14 | + |
| 15 | +``` |
| 16 | +Example 1: |
| 17 | +
|
| 18 | +Input: |
| 19 | +beginWord = "hit", |
| 20 | +endWord = "cog", |
| 21 | +wordList = ["hot","dot","dog","lot","log","cog"] |
| 22 | +
|
| 23 | +Output: |
| 24 | +[ |
| 25 | + ["hit","hot","dot","dog","cog"], |
| 26 | + ["hit","hot","lot","log","cog"] |
| 27 | +] |
| 28 | +
|
| 29 | +Example 2: |
| 30 | +
|
| 31 | +Input: |
| 32 | +beginWord = "hit" |
| 33 | +endWord = "cog" |
| 34 | +wordList = ["hot","dot","dog","lot","log"] |
| 35 | +
|
| 36 | +Output: [] |
| 37 | +
|
| 38 | +Explanation: The endWord "cog" is not in wordList, therefore no possible transformation. |
| 39 | +``` |
| 40 | + |
| 41 | +### Solutions |
| 42 | +* Method 1: dfs TLE |
| 43 | + ```Java |
| 44 | + class Solution { |
| 45 | + private int min = Integer.MAX_VALUE >> 1; |
| 46 | + public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) { |
| 47 | + List<List<String>> result = new ArrayList<>(); |
| 48 | + Set<String> words = new HashSet<>(wordList); |
| 49 | + if(!words.contains(endWord)) return result; |
| 50 | + int len = beginWord.length(); |
| 51 | + List<String> temp = new ArrayList<String>(); |
| 52 | + temp.add(beginWord); |
| 53 | + dfs(result, words, len, endWord, beginWord, temp); |
| 54 | + return result; |
| 55 | + } |
| 56 | + private void dfs(List<List<String>> result, Set<String> words, int len, String endWord, String cur, List<String> temp){ |
| 57 | + if(cur.equals(endWord)){ |
| 58 | + if(temp.size() < min){ |
| 59 | + result.clear(); |
| 60 | + min = temp.size(); |
| 61 | + } |
| 62 | + result.add(new ArrayList<String>(temp)); |
| 63 | + }else if(temp.size() < min){ |
| 64 | + for(int i = 0; i < len; i++){ |
| 65 | + char[] arr = cur.toCharArray(); |
| 66 | + for(char c = 'a'; c <= 'z'; c++){ |
| 67 | + arr[i] = c; |
| 68 | + String next = new String(arr); |
| 69 | + if(words.contains(next)){ |
| 70 | + words.remove(next); |
| 71 | + temp.add(next); |
| 72 | + dfs(result, words, len, endWord, next, temp); |
| 73 | + temp.remove(temp.size() - 1); |
| 74 | + words.add(next); |
| 75 | + } |
| 76 | + } |
| 77 | + arr[i] = cur.charAt(i); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + ``` |
| 83 | + |
| 84 | +* Method 2: bfs AC |
| 85 | + ```Java |
| 86 | + class Solution { |
| 87 | + private Map<String, List<String>> map; |
| 88 | + public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) { |
| 89 | + List<List<String>> result = new LinkedList<>(); |
| 90 | + Set<String> words = new HashSet<>(wordList); |
| 91 | + if(!words.contains(endWord)) return result; |
| 92 | + words.remove(beginWord); |
| 93 | + LinkedList<String> queue = new LinkedList<>(); |
| 94 | + map = new HashMap<>(); |
| 95 | + int len = beginWord.length(); |
| 96 | + boolean found = false; |
| 97 | + queue.add(beginWord); |
| 98 | + while(!queue.isEmpty() && !found){ |
| 99 | + int size = queue.size(); |
| 100 | + Set<String> curLevel = new HashSet<>(); |
| 101 | + for(int i = 0; i < size; i++){ |
| 102 | + String cur = queue.poll(); |
| 103 | + if(cur.equals(endWord)) found = true; |
| 104 | + char[] arr = cur.toCharArray(); |
| 105 | + for(int l = 0; l < len; l++){ |
| 106 | + for(char c = 'a'; c <= 'z'; c++){ |
| 107 | + if(c == cur.charAt(l)) continue; |
| 108 | + arr[l] = c; |
| 109 | + String next = new String(arr); |
| 110 | + if(words.contains(next) || curLevel.contains(next)){ |
| 111 | + map.put(cur, !map.containsKey(cur) ? new ArrayList<String>(): map.get(cur)); |
| 112 | + map.get(cur).add(next); |
| 113 | + curLevel.add(next); |
| 114 | + if(words.contains(next)) queue.offer(next); |
| 115 | + words.remove(next); |
| 116 | + } |
| 117 | + } |
| 118 | + arr[l] = cur.charAt(l); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + List<String> temp = new LinkedList<>(); |
| 123 | + temp.add(beginWord); |
| 124 | + dfs(result, temp, endWord, beginWord); |
| 125 | + return result; |
| 126 | + } |
| 127 | + private void dfs(List<List<String>> result, List<String> temp, String endWord, String cur){ |
| 128 | + if(cur.equals(endWord)){ |
| 129 | + result.add(new ArrayList<>(temp)); |
| 130 | + }else{ |
| 131 | + if(!map.containsKey(cur)) return; |
| 132 | + List<String> adj = map.get(cur); |
| 133 | + for(String s: adj){ |
| 134 | + temp.add(s); |
| 135 | + dfs(result, temp, endWord, s); |
| 136 | + temp.remove(temp.size() - 1); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + ``` |
0 commit comments