https://leetcode.com/problems/longest-palindromic-subsequence/description/
int longestPalindromeSubseq(string s) {
int n = s.size();
vector<vector<int>> dp(n, vector<int> (n, 0));
for(int j=0; j<n; j++) {
dp[j][j] = 1;
for(int i=j-1; i>=0; i--) {
if(s[i] == s[j]) dp[i][j] = dp[i+1][j-1] + 2;
else dp[i][j] = max(dp[i+1][j], dp[i][j-1]);
}
}
return dp[0][n-1];
}
No comments:
Post a Comment