Skip to content

Commit 59fb11b

Browse files
authored
Create 1048-longest-string-chain.js
1 parent a7417c3 commit 59fb11b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

1048-longest-string-chain.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string[]} words
3+
* @return {number}
4+
*/
5+
const longestStrChain = function(words) {
6+
words.sort((a, b) => a.length - b.length)
7+
const dp = {}
8+
for(let el of words) {
9+
dp[el] = 1
10+
}
11+
12+
let res = Number.MIN_VALUE
13+
for(let w of words) {
14+
for(let i = 0; i < w.length; i++) {
15+
let prev = w.slice(0, i) + w.slice(i + 1)
16+
dp[w] = Math.max(dp[w], (dp[prev] || 0) + 1 )
17+
}
18+
if(dp[w] > res) res = dp[w]
19+
}
20+
return res
21+
};

0 commit comments

Comments
 (0)