-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordLadder2.js
More file actions
95 lines (86 loc) · 2.45 KB
/
Copy pathWordLadder2.js
File metadata and controls
95 lines (86 loc) · 2.45 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @param {string} beginWord
* @param {string} endWord
* @param {string[]} wordList
* @return {string[][]}
*/
var findLadders = function(beginWord, endWord, wordList) {
if(!wordList.includes(endWord)) return [];
let answer = [];
let map = new Map();
wordList.forEach(word=> map.set(word, true));
if(!map.has(beginWord)) {
map.set(beginWord, true);
wordList.unshift(beginWord);
}
// 1. Construct a graph
let graph = new Map();
for(let word of wordList) {
if(!graph.has(word)) {
graph.set(word, new Node(word));
}
for(let ng of getNeighbors(map, word)) {
let nd = graph.get(ng);
if(!nd) {
nd = new Node(ng);
graph.set(ng, nd);
}
graph.get(word).neighbors.push(nd);
}
}
// 2. Assign distance for each node from start node.
let distance = -1;
let q = [graph.get(beginWord)];
while(q.length > 0) {
let size = q.length;
while(size--) {
let curNode = q.shift();
if(curNode.distance != -1) continue;
curNode.distance = distance+1;
for(let n of curNode.neighbors) {
if(n.distance === -1) q.push(n);
}
}
distance++;
}
console.log(graph);
// 3. Construct all the paths
let min = Infinity;
let stack = [ [graph.get(beginWord), []] ] ;
while(stack.length > 0) {
let [curNode, chain] = stack.pop();
if(curNode.val === endWord) {
min = Math.min(min, chain.length+1);
answer.push([...chain, curNode.val]);
}
else {
for(let nei of curNode.neighbors) {
if(nei.distance > curNode.distance) {
stack.push([nei, [...chain, curNode.val]]);
}
}
}
}
// 4. return only min sequences.
answer = answer.filter(arr => arr.length === min);
return answer;
};
function getNeighbors(map, word) {
let nei = []
for(let i=0;i<word.length;i++) {
for(let c of "abcdefghijklmnopqrstuvwxyz") {
let nw = word.slice(0, i) + c + word.slice(i+1);
if(map.has(nw) && word !== nw) {
nei.push(nw);
}
}
}
return nei;
}
class Node {
constructor(val) {
this.val = val;
this.neighbors = [];
this.distance = -1;
}
}