|
58 | 58 |
|
59 | 59 | <!-- 这里可写通用的实现逻辑 -->
|
60 | 60 |
|
| 61 | +DFS。 |
| 62 | + |
61 | 63 | <!-- tabs:start -->
|
62 | 64 |
|
63 | 65 | ### **Python3**
|
64 | 66 |
|
65 | 67 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 68 |
|
67 | 69 | ```python
|
| 70 | +class Solution: |
| 71 | + def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: |
| 72 | + def dfs(path, cur): |
| 73 | + if cur == beginWord: |
| 74 | + ans.append(path[::-1]) |
| 75 | + return |
| 76 | + for precursor in prev[cur]: |
| 77 | + path.append(precursor) |
| 78 | + dfs(path, precursor) |
| 79 | + path.pop() |
68 | 80 |
|
| 81 | + ans = [] |
| 82 | + words = set(wordList) |
| 83 | + if endWord not in words: |
| 84 | + return ans |
| 85 | + words.discard(beginWord) |
| 86 | + dist = {beginWord: 0} |
| 87 | + prev = defaultdict(set) |
| 88 | + q = deque([beginWord]) |
| 89 | + found = False |
| 90 | + step = 0 |
| 91 | + while q and not found: |
| 92 | + step += 1 |
| 93 | + for i in range(len(q), 0, -1): |
| 94 | + p = q.popleft() |
| 95 | + s = list(p) |
| 96 | + for i in range(len(s)): |
| 97 | + ch = s[i] |
| 98 | + for j in range(26): |
| 99 | + s[i] = chr(ord('a') + j) |
| 100 | + t = ''.join(s) |
| 101 | + if dist.get(t, 0) == step: |
| 102 | + prev[t].add(p) |
| 103 | + if t not in words: |
| 104 | + continue |
| 105 | + prev[t].add(p) |
| 106 | + words.discard(t) |
| 107 | + q.append(t) |
| 108 | + dist[t] = step |
| 109 | + if endWord == t: |
| 110 | + found = True |
| 111 | + s[i] = ch |
| 112 | + if found: |
| 113 | + path = [endWord] |
| 114 | + dfs(path, endWord) |
| 115 | + return ans |
69 | 116 | ```
|
70 | 117 |
|
71 | 118 | ### **Java**
|
72 | 119 |
|
73 | 120 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
74 | 121 |
|
75 | 122 | ```java
|
| 123 | +class Solution { |
| 124 | + private List<List<String>> ans; |
| 125 | + private Map<String, Set<String>> prev; |
| 126 | + |
| 127 | + public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) { |
| 128 | + ans = new ArrayList<>(); |
| 129 | + Set<String> words = new HashSet<>(wordList); |
| 130 | + if (!words.contains(endWord)) { |
| 131 | + return ans; |
| 132 | + } |
| 133 | + words.remove(beginWord); |
| 134 | + Map<String, Integer> dist = new HashMap<>(); |
| 135 | + dist.put(beginWord, 0); |
| 136 | + prev = new HashMap<>(); |
| 137 | + Queue<String> q = new ArrayDeque<>(); |
| 138 | + q.offer(beginWord); |
| 139 | + boolean found = false; |
| 140 | + int step = 0; |
| 141 | + while (!q.isEmpty() && !found) { |
| 142 | + ++step; |
| 143 | + for (int i = q.size(); i > 0; --i) { |
| 144 | + String p = q.poll(); |
| 145 | + char[] chars = p.toCharArray(); |
| 146 | + for (int j = 0; j < chars.length; ++j) { |
| 147 | + char ch = chars[j]; |
| 148 | + for (char k = 'a'; k <= 'z'; ++k) { |
| 149 | + chars[j] = k; |
| 150 | + String t = new String(chars); |
| 151 | + if (dist.getOrDefault(t, 0) == step) { |
| 152 | + prev.get(t).add(p); |
| 153 | + } |
| 154 | + if (!words.contains(t)) { |
| 155 | + continue; |
| 156 | + } |
| 157 | + prev.computeIfAbsent(t, key -> new HashSet<>()).add(p); |
| 158 | + words.remove(t); |
| 159 | + q.offer(t); |
| 160 | + dist.put(t, step); |
| 161 | + if (endWord.equals(t)) { |
| 162 | + found = true; |
| 163 | + } |
| 164 | + } |
| 165 | + chars[j] = ch; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + if (found) { |
| 170 | + Deque<String> path = new ArrayDeque<>(); |
| 171 | + path.add(endWord); |
| 172 | + dfs(path, beginWord, endWord); |
| 173 | + } |
| 174 | + return ans; |
| 175 | + } |
| 176 | + |
| 177 | + private void dfs(Deque<String> path, String beginWord, String cur) { |
| 178 | + if (cur.equals(beginWord)) { |
| 179 | + ans.add(new ArrayList<>(path)); |
| 180 | + return; |
| 181 | + } |
| 182 | + for (String precursor : prev.get(cur)) { |
| 183 | + path.addFirst(precursor); |
| 184 | + dfs(path, beginWord, precursor); |
| 185 | + path.removeFirst(); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +### **Go** |
76 | 192 |
|
| 193 | +```go |
| 194 | +func findLadders(beginWord string, endWord string, wordList []string) [][]string { |
| 195 | + var ans [][]string |
| 196 | + words := make(map[string]bool) |
| 197 | + for _, word := range wordList { |
| 198 | + words[word] = true |
| 199 | + } |
| 200 | + if !words[endWord] { |
| 201 | + return ans |
| 202 | + } |
| 203 | + words[beginWord] = false |
| 204 | + dist := map[string]int{beginWord: 0} |
| 205 | + prev := map[string]map[string]bool{} |
| 206 | + q := []string{beginWord} |
| 207 | + found := false |
| 208 | + step := 0 |
| 209 | + for len(q) > 0 && !found { |
| 210 | + step++ |
| 211 | + for i := len(q); i > 0; i-- { |
| 212 | + p := q[0] |
| 213 | + q = q[1:] |
| 214 | + chars := []byte(p) |
| 215 | + for j := 0; j < len(chars); j++ { |
| 216 | + ch := chars[j] |
| 217 | + for k := 'a'; k <= 'z'; k++ { |
| 218 | + chars[j] = byte(k) |
| 219 | + t := string(chars) |
| 220 | + if v, ok := dist[t]; ok { |
| 221 | + if v == step { |
| 222 | + prev[t][p] = true |
| 223 | + } |
| 224 | + } |
| 225 | + if !words[t] { |
| 226 | + continue |
| 227 | + } |
| 228 | + if len(prev[t]) == 0 { |
| 229 | + prev[t] = make(map[string]bool) |
| 230 | + } |
| 231 | + prev[t][p] = true |
| 232 | + words[t] = false |
| 233 | + q = append(q, t) |
| 234 | + dist[t] = step |
| 235 | + if endWord == t { |
| 236 | + found = true |
| 237 | + } |
| 238 | + } |
| 239 | + chars[j] = ch |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + var dfs func(path []string, begin, cur string) |
| 244 | + dfs = func(path []string, begin, cur string) { |
| 245 | + if cur == beginWord { |
| 246 | + cp := make([]string, len(path)) |
| 247 | + copy(cp, path) |
| 248 | + ans = append(ans, cp) |
| 249 | + return |
| 250 | + } |
| 251 | + for k := range prev[cur] { |
| 252 | + path = append([]string{k}, path...) |
| 253 | + dfs(path, beginWord, k) |
| 254 | + path = path[1:] |
| 255 | + } |
| 256 | + } |
| 257 | + if found { |
| 258 | + path := []string{endWord} |
| 259 | + dfs(path, beginWord, endWord) |
| 260 | + } |
| 261 | + return ans |
| 262 | +} |
77 | 263 | ```
|
78 | 264 |
|
79 | 265 | ### **...**
|
|
0 commit comments