Skip to content

Commit

Permalink
Merge pull request #410 from nandanabhishek/patch-1
Browse files Browse the repository at this point in the history
added LongestPalindromicSubsequence.py
  • Loading branch information
Sayansree committed Oct 1, 2021
2 parents a0f0c0f + 2de2e86 commit a859700
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions 21. Dynamic Programming/LongestPalindromicSubsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public:
// Bottom-up approach
// TC: O(N*N), SC: O(N*N)
int longestPalindromeSubseqTabular(string& s) {
if(s.empty())
return 0;

const int N = s.size();
// dp(i, j): length of longest palindromic substring in s[i:j]
vector<vector<int> > dp(N, vector<int>(N, 0));

// all single chars are palindromic
for(int i = 0; i < N; i++)
dp[i][i] = 1;

for(int l = 1; l < N; l++) {
// starting index of window
for(int i = 0; i < N - l; i++) {
// ending index of window
int j = i + l;

// if there are only two chars
if((j - i + 1) == 2) {
// if the chars are same, then that contributes 2, otherwise since
// individually they are palindromic so max length 1
dp[i][j] = 1 + (s[i] == s[j]);
}
else {
// for s[i:j]:
// longest palindrome length in s[i+1 : j-1] and +1 if s[i] == s[j]
// if s[i] != s[j], longest length palindrome = Max of longest palin in s[i:j-1] and s[i+1:j]
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];
}

0 comments on commit a859700

Please sign in to comment.