Skip to content

Commit dd6f7ce

Browse files
authored
Update 516-longest-palindromic-subsequence.js
1 parent d45b4fc commit dd6f7ce

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

516-longest-palindromic-subsequence.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const longestPalindromeSubseq = function(s) {
6+
const n = s.length
7+
const dp = Array.from({ length: n }, () => Array(n).fill(0))
8+
for(let i = 0; i < n; i++) {
9+
dp[i][i] = 1
10+
for(let j = i - 1; j >= 0; j--) {
11+
if(s[i] === s[j]) {
12+
dp[i][j] = dp[i - 1][j + 1] + 2
13+
} else {
14+
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j + 1])
15+
}
16+
}
17+
}
18+
19+
return dp[n - 1][0]
20+
};
21+
22+
// another
23+
124
/**
225
* @param {string} s
326
* @return {number}

0 commit comments

Comments
 (0)