Skip to content

Commit

Permalink
Added Longest Palindromic Subsequence code
Browse files Browse the repository at this point in the history
Added Longest Palindromic Subsequence code in C++.
  • Loading branch information
PulkitKhagta authored Oct 2, 2020
1 parent a17ea88 commit 985d0b4
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions LongestPalindromicSubsequence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <bits/stdc++.h>

using namespace std;
int LPS(string s1){
string s2 = s1;
reverse(s2.begin(), s2.end());
vector<vector<int>> dp(s1.length()+1, vector<int>(s2.length()+1, 0));
for(int i = 1; i<=s1.length(); i++){
for(int j = 1; j<= s2.length(); j++){
if(s1[i-1] == s2[j-1])
dp[i][j] = 1+ dp[i-1][j-1];
else
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
return dp[s1.length()][s2.length()];
}
int main()
{
string s1;
cin>>s1;
int res = LPS(s1);
cout<< res<<endl;
return 0;
}

0 comments on commit 985d0b4

Please sign in to comment.