-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path516.js
More file actions
22 lines (20 loc) · 699 Bytes
/
516.js
File metadata and controls
22 lines (20 loc) · 699 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var longestPalindromeSubseq = function(s) {
let dp = [];//dp[i][j]: longest palindrome subsequence in substring i,j
return longestPalindromeSub(s,0,s.length-1,dp);
};
var longestPalindromeSub = function(s,i,j,dp){
dp[i+1]=dp[i+1]||[];
dp[i]=dp[i]||[];
if(dp[i][j])return dp[i][j];
if(i===j)return 1;
if(i+1===j){if(s[i]===s[j])return 2;else return 1;}
if(s[i]===s[j]){
dp[i+1][j-1]= longestPalindromeSub(s,i+1,j-1,dp);
dp[i][j] = dp[i+1][j-1]+2;
}else{
dp[i+1][j]= longestPalindromeSub(s,i+1,j,dp);
dp[i][j-1]= longestPalindromeSub(s,i,j-1,dp);
dp[i][j] = Math.max(dp[i+1][j],dp[i][j-1]);
}
return dp[i][j]
}