这题思路跑偏了, 最开始想的是建一棵树来做, 写了一一堆代码, 还只能通过部分 case
动态规划解很优雅, 这个题很舒服
/**
* @param {string[]} words
* @return {number}
*/
const longestStrChain = function (words) {
const f = [];
let ans = 0;
words.sort((a, b) => {
return a.length - b.length;
})
for (w of words) {
l = w.length
for (let i = 1; i <= l; ++i) {
f[w.slice(0, i - 1) + w.slice(i)] = f[w.slice(0, i - 1) + w.slice(i)] || 0
f[w] = f[w] || 0
f[w] = Math.max(f[w], f[w.slice(0, i - 1) + w.slice(i)] + 1)
ans = Math.max(ans, f[w])
}
}
return ans
};