forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.ts
46 lines (43 loc) · 1.09 KB
/
Solution2.ts
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
function wordBreak(s: string, wordDict: string[]): boolean {
const trie = new Trie();
for (const w of wordDict) {
trie.insert(w);
}
const n = s.length;
const f: boolean[] = new Array(n + 1).fill(false);
f[n] = true;
for (let i = n - 1; i >= 0; --i) {
let node: Trie = trie;
for (let j = i; j < n; ++j) {
const k = s.charCodeAt(j) - 97;
if (!node.children[k]) {
break;
}
node = node.children[k];
if (node.isEnd && f[j + 1]) {
f[i] = true;
break;
}
}
}
return f[0];
}
class Trie {
children: Trie[];
isEnd: boolean;
constructor() {
this.children = new Array(26);
this.isEnd = false;
}
insert(w: string): void {
let node: Trie = this;
for (const c of w) {
const i = c.charCodeAt(0) - 97;
if (!node.children[i]) {
node.children[i] = new Trie();
}
node = node.children[i];
}
node.isEnd = true;
}
}