-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathWord Ladder II.js
98 lines (80 loc) · 2.66 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Runtime: 101 ms (Top 95.15%) | Memory: 44.2 MB (Top 99.56%)
const isMatch = (currWord, nextWord) => {
let mismatch = 0;
for(let i = 0; i < nextWord.length; i += 1) {
if(nextWord[i] !== currWord[i]) {
mismatch += 1;
}
}
return mismatch === 1;
}
const getNextWords = (lastRung, dictionary) => {
const nextWords = [];
for(const word of dictionary) {
if(isMatch(word, lastRung)) {
nextWords.push(word);
}
}
return nextWords;
}
const updateLadders = (ladders, dictionary) => {
const updatedLadders = [];
const nextRung = new Set();
for(const ladder of ladders) {
const nextWords = getNextWords(ladder[ladder.length - 1], dictionary);
for(const nextWord of nextWords) {
updatedLadders.push([...ladder, nextWord]);
nextRung.add(nextWord);
}
}
return [updatedLadders, nextRung];
}
const updateDictionary = (dictionary, nextRung) => {
return dictionary.filter((word) => !nextRung.has(word));
}
// BFS traversal from endWord to beginWord
// This limits the paths that we'll need to consider during our traversal from beingWord to endWord
const getDictionary = (wordList, endWord, beginWord) => {
const dictionary = new Set();
let currRung = [endWord];
while(currRung.length > 0) {
const nextRung = new Set();
if(!wordList.includes(beginWord)) break;
while(currRung.length > 0) {
const currWord = currRung.pop();
dictionary.add(currWord);
for(const nextWord of wordList) {
if(isMatch(currWord, nextWord)) {
nextRung.add(nextWord);
}
}
}
currRung = [...nextRung];
wordList = wordList.filter((word) => !nextRung.has(word));
}
return [...dictionary];
}
var findLadders = function(beginWord, endWord, wordList) {
if(!wordList.includes(endWord)) return [];
if(!wordList.includes(beginWord)) wordList.push(beginWord);
const result = [];
const saveResult = (ladders) => {
for(const ladder of ladders) {
if(ladder[ladder.length - 1] === endWord) {
result.push(ladder);
}
}
}
let ladders = [[beginWord]];
let dictionary = getDictionary(wordList, endWord, beginWord);
while(ladders.length > 0) {
if(!dictionary.includes(endWord)) {
saveResult(ladders);
break;
}
const [updatedLadders, nextRung] = updateLadders(ladders, dictionary);
ladders = updatedLadders;
dictionary = updateDictionary(dictionary, nextRung);
}
return result;
};