Skip to content

Commit 47e179f

Browse files
authored
Merge pull request #41 from masx200/longest-palindromic-subsequence
longestPalindromeSubseq
2 parents 42ef35b + 22ead17 commit 47e179f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Step 2. Add the dependency
4949

5050
<summary>展开查看</summary>
5151

52+
https://leetcode-cn.com/problems/longest-palindromic-subsequence/
53+
5254
https://leetcode.cn/problems/minimum-number-of-operations-to-reinitialize-a-permutation/
5355

5456
https://leetcode.cn/problems/palindromic-substrings/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function longestPalindromeSubseq(s: string): number {
2+
const n = s.length;
3+
const dp: number[][] = new Array(n).fill(0).map(() => new Array(n).fill(0));
4+
for (let i = n - 1; i >= 0; i--) {
5+
dp[i][i] = 1;
6+
const c1 = s[i];
7+
for (let j = i + 1; j < n; j++) {
8+
const c2 = s[j];
9+
if (c1 === c2) {
10+
dp[i][j] = dp[i + 1][j - 1] + 2;
11+
} else {
12+
dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
13+
}
14+
}
15+
}
16+
return dp[0][n - 1];
17+
}
18+
19+
export default longestPalindromeSubseq;

0 commit comments

Comments
 (0)