-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0139-word-break.js
179 lines (141 loc) · 5.12 KB
/
0139-word-break.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/**
* Brute Force - DFS
* Hash Set - Distinct Keys
* Time O(2^N) | Space O(N)
* https://leetcode.com/problems/word-break/
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = (s, wordDict) => {
const wordSet = new Set(wordDict);/* Time O(N) | Space O(N) */
return canBreak(s, wordSet); /* Time O(2^N) | Space O(N) */
};
var canBreak = (s, wordSet, start = 0) => {
const isBaseCase = (start === s.length);
if (isBaseCase) return true;
return dfs(s, wordSet, start);/* Time O(2^N) | Space O(N) */
}
var dfs = (s, wordSet, start) => {
for (let end = (start + 1); end <= s.length; end++) {/* Time O(N) */
const word = s.slice(start, end); /* Time O(N) | Space O(N) */
const _canBreak = wordSet.has(word)
&& canBreak(s, wordSet, end); /* Time O(2^N) | Space O(N) */
if (_canBreak) return true;
}
return false;
}
/**
* DP - Top Down
* Array - Memoization
* Hash Set - Distinct Keys
* Time O(N^3) | Space O(N)
* https://leetcode.com/problems/word-break/
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = (s, wordDict) => {
const wordSet = new Set(wordDict); /* Time O(N) | Space O(N) */
const memo = new Array(s.length).fill(null); /* | Space O(N) */
const start = 0;
return canBreak(s, wordSet, start, memo); /* Time O(N * N * N) | Space O(N) */
}
var canBreak = (s, wordSet, start, memo) => {
const isBaseCase1 = (s.length === start);
if (isBaseCase1) return true;
const hasSeen = (memo[start] !== null);
if (hasSeen) return memo[start];
return dfs(s, wordSet, start, memo);/* Time O(N * N * N) | Space O(N) */
}
var dfs = (s, wordSet, start, memo) => {
for (let end = (start + 1); (end <= s.length); end++) {/* Time O(N) */
const word = s.slice(start, end); /* Time O(N) | Space O(N) */
const _canBreak = wordSet.has(word)
&& canBreak(s, wordSet, end, memo); /* Time O(N * N) */
if (_canBreak) {
memo[start] = true;
return true;
}
}
memo[start] = false;
return false;
}
/**
* DP - Bottom Up
* Array - Tabulation
* Hash Set - Distinct Keys
* Time O(N^3) | Space O(N)
* https://leetcode.com/problems/word-break/
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = (s, wordDict) => {
const wordSet = new Set(wordDict);/* Time O(N) | Space O(N) */
const tabu = initTabu(s); /* | Space O(N) */
canBreak(s, wordSet, tabu); /* Time O(N * N * N) | Space O(N) */
return tabu[s.length];
}
const initTabu = (s) => {
const tabu = new Array((s.length + 1)).fill(false);/* Space O(N) */
tabu[0] = true;
return tabu;
}
var canBreak = (s, wordSet, tabu) => {
for (let end = 1; (end <= s.length); end++) {/* Time O(N) */
checkWord(s, wordSet, end, tabu); /* Time O(N * N) | Space O(N) */
}
}
var checkWord = (s, wordSet, end, tabu) => {
for (let start = 0; (start < end); start++) {/* Time O(N) */
const word = s.slice(start, end); /* Time O(N) | Space O(N) */
const canBreak = tabu[start] && wordSet.has(word);
if (!canBreak) continue;
tabu[end] = true;
return;
}
}
/**
* Tree Traversal - BFS
* Queue - Level Order Space O(WIDTH)
* Hash Set - Distinct Keys
* Array - Seen
* Time O(N^3) | Space O(N)
* https://leetcode.com/problems/word-break/
* @param {string} s
* @param {string[]} wordDict
* @return {boolean}
*/
var wordBreak = function(s, wordDict) {
const wordSet = new Set(wordDict); /* Time O(N) | Space O(N) */
const queue = new Queue([ 0 ]); /* | Space O(N) */
const seen = new Array(s.length).fill(false);/* | Space O(N) */
return bfs(queue, s, wordSet, seen); /* Time O(N * N * N) | Space O(N + WIDTH) */
}
const bfs = (queue, s, wordSet, seen) => {
while (!queue.isEmpty()) {
for (let level = (queue.size() - 1); (0 <= level); level--) {/* Time O(N) */
if (canWordBreak(queue, s, wordSet, seen)) return true; /* Time O(N * N) | Space O(N + WIDTH) */
}
}
return false;
}
var canWordBreak = (queue, s, wordSet, seen) => {
const start = queue.dequeue();
const hasSeen = seen[start];
if (hasSeen) return false;
if (canBreak(queue, s, start, wordSet)) return true;/* Time O(N * N) | Space O(N + WIDTH) */
seen[start] = true; /* | Space O(N) */
return false;
}
var canBreak = (queue, s, start, wordSet) => {
for (let end = start + 1; end <= s.length; end++) {/* Time O(N) */
const word = s.slice(start, end); /* Time O(N) | Space O(N) */
if (!wordSet.has(word)) continue;
queue.enqueue(end); /* | Space O(WIDTH) */
const _canBreak = end === s.length;
if (_canBreak) return true;
}
return false
}