forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcatenated Words.js
92 lines (79 loc) · 2.02 KB
/
Concatenated Words.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
// Runtime: 578 ms (Top 25.39%) | Memory: 72.5 MB (Top 21.92%)
class Node {
val;
children;
isWord = false;
constructor(val) {
this.val = val;
}
}
class Trie {
nodes = Array(26);
addWord(w) {
let idx = this.getIdx(w[0]);
let node = this.nodes[idx] || new Node(w[0]);
this.nodes[idx] = node;
for (let i=1; i < w.length; ++i) {
node.children = node.children || Array(26);
idx = this.getIdx(w[i]);
let childNode = node.children[idx] || new Node(w[i]);
node.children[idx] = childNode;
node = childNode;
}
node.isWord = true;
}
getExistingWords(w, start) {
const rslt = [];
let node = {children: this.nodes};
for (let i=start; i < w.length; ++i) {
node = (node.children || [])[this.getIdx(w[i])];
if (!node) {
break;
}
if (node.isWord) {
rslt.push(i-start+1);
}
}
return rslt;
}
getIdx(ch) {
return ch.charCodeAt(0) - "a".charCodeAt(0);
}
}
var findAllConcatenatedWordsInADict = function(words) {
const rslt = [];
words = words.sort((a,b) => a.length-b.length);
let start = 0;
if (words[0].length === 0) {
++start;
}
const tr = new Trie();
for (let i = start; i < words.length; ++i) {
if (check(words[i], 0, tr, Array(words[i].length))) {
rslt.push(words[i]);
}
tr.addWord(words[i]);
}
return rslt;
};
function check(word, i, trie, dp) {
if (i > word.length || dp[i] === false) {
return false;
}
if (i === word.length || dp[i] === true) {
return true;
}
const lens = trie.getExistingWords(word, i);
if (!lens.length) {
dp[i] = false;
return false;
}
dp[i] = true;
for (let l of lens) {
if (check(word, i+l, trie, dp)) {
return true;
}
}
dp[i] = false;
return false;
}