-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
47 lines (46 loc) · 1.26 KB
/
solution.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
/**
* @param {string[]} words
* @return {number}
*/
// 很像最长增长子序列
var longestStrChain = function (words) {
let result = 1;
// 按照长度排序
words.sort((a, b) => a.length - b.length);
const dp = new Array(words.length).fill(1);
for (let i = 1; i < words.length; i++) {
const L = words[i].length;
for (let j = i - 1; j > -1; j--) {
if (words[j].length === L) {
continue;
} else if (words[j].length === L - 1) {
// 长度-1的且构成前体的 找最大值
if (isPredecessor(words[j], words[i])) {
dp[i] = Math.max(dp[j] + 1, dp[j]);
result = Math.max(dp[i], result);
}
} else {
break;
}
}
}
return result;
};
// 判断是否是Predecessor
function isPredecessor (word1, word2) {
let index1 = 0;
let index2 = 0;
let diff = false;
while (index1 < word1.length && index2 < word2.length) {
if (word1[index1] === word2[index2]) {
index1++;
index2++;
} else if (diff) {
return false;
} else {
diff = true;
index2++;
}
}
return true;
}