-
Notifications
You must be signed in to change notification settings - Fork 481
/
1048.js
29 lines (28 loc) · 795 Bytes
/
1048.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
var longestStrChain = function(words) {
let isPreString = function(s1, s2) {
let p = 0, n = s1.length;
for (let i = 0; i < n; i++) {
if (s1[i] != s2[i + p]) {
if (++p != 1) return false;
else --i;
}
}
return true;
}
let mem = new Array(17), res = 1;
for (let i = 0; i <= 16; i++) mem[i] = [];
for (let w of words) {
mem[w.length].push([w, 1]);
}
for (let i = 2; i <= 16; i++) {
for (let s2 of mem[i]) {
for (let s1 of mem[i-1]) {
if (isPreString(s1[0], s2[0])) {
s2[1] = Math.max(s2[1], s1[1] + 1);
}
res = Math.max(res, s2[1]);
}
}
}
return res;
};