-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathword-ladder-ii.js
79 lines (71 loc) · 1.79 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
var DEBUG = process.env.DEBUG;
function getPaths(state, curr, path, paths) {
path.unshift(curr);
if (state[curr].p === null) {
paths.push(path.slice(0));
} else {
state[curr].p.forEach(p => getPaths(state, p, path, paths));
}
path.shift(curr);
}
/**
* @param {string} start
* @param {string} end
* @param {set} dict
* @return {string[][]}
*/
var findLadders = function(start, end, dict) {
dict.add(start);
dict.add(end);
// constructGraph(dict);
var state = {};
// bfs
var queue = [];
queue.push(start);
state[start] = {
depth: 1,
p: null
};
while (queue.length) {
var curr = queue.shift();
var sword = curr.split('');
for (var j = 0; j < curr.length; j++) {
for (var k = 'a'.charCodeAt(0); k <= 'z'.charCodeAt(0); k++) {
var tmp = sword[j];
sword[j] = String.fromCharCode(k);
var newword = sword.join('');
if (dict.has(newword) && newword !== curr) {
var next = newword;
if (!state[next]) {
queue.push(next);
state[next] = {
depth: state[curr].depth + 1,
p: [curr]
};
} else {
if (state[next].depth === state[curr].depth + 1)
state[next].p.push(curr);
}
}
sword[j] = tmp;
}
}
}
if (!state[end]) return [];
var ans = [];
getPaths(state, end, [], ans);
return ans;
};
function test(f) {
[
["hit", "cog", new Set(["hot","dot","dog","lot","log"])],
["hot", "dog", new Set(["hot","dog"])],
["a", "c", new Set(["a", "b", "c"])],
["a", "c", new Set(["b"])],
require('./word-ladder-ii-in1.js'),
require('./word-ladder-ii-in2.js')
].forEach(function (input) {
console.log(f.apply(undefined, input));
});
}
if (DEBUG) test(findLadders);