-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path127.js
More file actions
42 lines (38 loc) · 1.23 KB
/
127.js
File metadata and controls
42 lines (38 loc) · 1.23 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
/**
* @param {string} beginWord
* @param {string} endWord
* @param {string[]} wordList
* @return {number}
*/
var ladderLength = function(beginWord, endWord, wordList) {
var wordHash = {};
wordList.forEach(function(w){ wordHash[w] = true; });
if (!wordHash[endWord]) return 0;
const getSimilarWords = word => {
const similarWords = [];
for (let i = 0; i < word.length; ++i) {
var alphabets = "abcdefghijklmnopqrstuvwxyz";
for (let j = 0; j < 26; ++j) {
if (alphabets[j] == word[i]) continue;
const similarWord = word.substring(0, i) + alphabets[j] + word.substring(i + 1);
if (wordHash[similarWord]) similarWords.push(similarWord);
}
}
return similarWords;
}
const depthHash = {};
depthHash[beginWord] = 1;
const array = [beginWord];
while(array.length > 0) {
const currentWord = array.shift();
const similarWords = getSimilarWords(currentWord, wordHash);
for (let i = 0; i < similarWords.length; ++i) {
const similarWord = similarWords[i];
if (similarWord == endWord) return depthHash[currentWord] + 1;
depthHash[similarWord] = depthHash[currentWord] + 1;
delete wordHash[similarWord];
array.push(similarWord);
}
}
return 0;
};