Skip to content

Commit

Permalink
Solution3: Reverse string
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhansu-kr committed Jul 11, 2022
1 parent e408129 commit 84dfe28
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions 31_LongestPalindromicSubSeq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
#include <bits/stdc++.h>
using namespace std;

class Solution3
{
// Tabulation : Reverse string
public:
int longestPalindromeSubseq(string s)
{
// We just need to find the longest common subsequence
// in string s and rev(s);

string t = s;
reverse(s.begin(), s.end());

int n1 = s.size(), n2 = t.size();
vector<int> cp(n2 + 1), xp(n2 + 1);

for (int i = 1; i <= n1; ++i)
{
for (int j = 1; j <= n2; ++j)
{
if (s[i - 1] == t[j - 1]) xp[j] = 1 + cp[j - 1];
else xp[j] = max(xp[j - 1], cp[j]);
}
cp = xp;
}
return cp[n2];
}
};

class Solution2
{
// Tabulation
Expand Down

0 comments on commit 84dfe28

Please sign in to comment.