728x90
function longestChain(words) {
const dp = {};
words.sort((a,b)=>a.length - b.length);
let max = 0;
for(const word of words){
let longest = 0;
for(let i =0; i< word.length; i++){
const sub = word.slice(0,i) + word.slice(i+1);
longest = Math.max(longest, (dp[sub] || 0)+1);
}
dp[word]=longest;
max = Math.max(max, longest);
}
return max;
}
728x90