forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
127 Word Ladder.js
141 lines (102 loc) · 4.29 KB
/
127 Word Ladder.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
// Only one letter can be changed at a time
// Each intermediate word must exist in the word list
// For example,
// Given:
// beginWord = "hit"
// endWord = "cog"
// wordList = ["hot","dot","dog","lot","log"]
// As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
// return its length 5.
// Note:
// Return 0 if there is no such transformation sequence.
// All words have the same length.
// All words contain only lowercase alphabetic characters.
// Amazon LinkedIn Snapchat Facebook Yelp
// Leetcode 127
// Language: Javascript
// Problem: https://leetcode.com/problems/word-ladder/
// Author: Chihung Yu
/**
* @param {string} beginWord
* @param {string} endWord
* @param {Set} wordList
* Note: wordList is a Set object, see:
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
* @return {number}
*/
var ladderLength = function(beginWord, endWord, wordList) {
var visited = new Set();
var queue = [];
var level = 1;
var letters = 'abcdefghijklmnopqrstuvwxyz';
queue.push(beginWord);
visited.add(beginWord);
while(queue.length > 0) {
var len = queue.length;
for(var i = 0; i < len; i++) {
var word = queue.shift();
for(var j = 0; j < word.length; j++) {
for(var k = 0; k < letters.length; k++) {
var newWord = word.substring(0, j) + letters[k] + word.substring(j + 1);
if(newWord === endWord) {
return level + 1;
}
if(wordList.has(newWord) && !visited.has(newWord)) {
queue.push(newWord);
visited.add(newWord);
}
}
}
}
level++;
}
return 0;
};
// will time exceeded. javascript hash is slower than set
var ladderLength = function(beginWord, endWord, wordList) {
if(beginWord === endWord) {
return 0;
}
var queue = [];
var visited = {};
var count = 1;
var baseCharCode = 'a'.charCodeAt(0);
queue.push(beginWord);
while(queue.length) {
var len = queue.length;
for(var i = 0; i < len; i++) {
var word = queue.shift();
for(var j = 0; j < word.length; j++) {
for(var k = 0; k < 26; k++) {
var newChar = String.fromCharCode(baseCharCode + k);
var newWord = word.substring(0, j) + newChar + word.substring(j + 1);
if(newWord === endWord) {
return count + 1;
}
if(!visited[newWord] && wordList.has(newWord)) {
visited[newWord] = true;
queue.push(newWord);
}
}
}
}
count++;
}
return 0;
};
Hi Thiago
I very much appreciate that you took the time writing this warm welcoming letter and provided me the opportunity to come onsite visiting the team at Periscope.
After much thought, I've decided to accept offer at another company. It was really a tough call for me since I really like the product, role and people I met during my visit.
Again, I cannot thank you enough for your time, and support. It's been a great pleasure to know you and the team. I hope that we cross paths in the near future.
Wish you, teams, and Periscope all the success.
Regards,
Jerry
Hi Cynthia
Thank your for patience and support along the way.
I very much appreciate that you took the time answering many of my questions about the Periscope, and role.
After much thought, I've decided to accept offer at another company. It was really a tough call for me since I really like the product and people I met during my visit.
Again, I cannot thank you enough for your time, and support. It's been a great pleasure to know you and the team. I hope that we cross paths in the near future.
Wish you, teams, and Periscope all the success.
Regards,
Jerry