-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #410 from nandanabhishek/patch-1
added LongestPalindromicSubsequence.py
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |