-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy path126-word-ladder-ii.js
64 lines (64 loc) · 1.72 KB
/
126-word-ladder-ii.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @param {string} beginWord
* @param {string} endWord
* @param {string[]} wordList
* @return {string[][]}
*/
const findLadders = function (beginWord, endWord, wordList) {
const res = []
if (!wordList.includes(endWord)) return res
const set1 = new Set([beginWord]),
set2 = new Set([endWord]),
wordSet = new Set(wordList),
temp = [beginWord]
const map = new Map()
const traverse = (set1, set2, dir) => {
if (set1.size === 0) return false
if (set1.size > set2.size) return traverse(set2, set1, !dir)
for (const val of set1.values()) {
if (wordSet.has(val)) wordSet.delete(val)
}
for (const val of set2.values()) {
if (wordSet.has(val)) wordSet.delete(val)
}
const set = new Set()
let done = false
for (const str of set1.values()) {
for (let i = 0; i < str.length; i++) {
for (let ch = 'a'.charCodeAt(); ch <= 'z'.charCodeAt(); ch++) {
const word =
str.slice(0, i) + String.fromCharCode(ch) + str.slice(i + 1)
const key = dir ? str : word
const val = dir ? word : str
const list = map.get(key) || []
if (set2.has(word)) {
done = true
list.push(val)
map.set(key, list)
}
if (!done && wordSet.has(word)) {
set.add(word)
list.push(val)
map.set(key, list)
}
}
}
}
return done || traverse(set2, set, !dir)
}
const dfs = (word) => {
if (word === endWord) {
res.push(temp.slice())
return
}
const nei = map.get(word) || []
for (const w of nei) {
temp.push(w)
dfs(w)
temp.pop()
}
}
traverse(set1, set2, true)
dfs(beginWord)
return res
}