-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path26. Longest Palindromic Subsequence.cpp
86 lines (59 loc) · 1.72 KB
/
26. Longest Palindromic Subsequence.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
26 .Longest Palindromic Subsequence
Video Link : https://youtu.be/wuOOOATz_IA
GFG problem link : https://practice.geeksforgeeks.org/problems/longest-palindromic-subsequence-1612327878/1
Code : -------------->>>>>>>>>>>>>>>>>>>>
Approach: i). make another string identical to given one ;
ii). Reverse that string and
iii). then find the length of longest common subsequence
----------------------xxxxxxxxxxxxx---------------xxxxxxxxxxxxx_________CODE___________xxxxxxxxxxxxxxx---------------------xxxxxxxxxxxxxxx----------
*/
//{ Driver Code Starts
//Initial template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function Template for C++
class Solution{
public:
int LCS(string s1,string s2,int n,int m, vector<vector<int>> &t)
{
if(n==0 || m==0)
{
return 0;
}
if(t[n][m]!=-1)
{
return t[n][m];
}
if(s1[n-1]==s2[m-1])
{
return t[n][m]= 1+LCS(s1,s2,n-1,m-1,t);
}
else{
return t[n][m]= max(LCS(s1,s2,n-1,m,t), LCS(s1,s2,n,m-1,t));
}
}
int longestPalinSubseq(string A) {
//code here
string s1=A;
string s2= A;
reverse(s2.begin(),s2.end());
int n=A.size(),m=n;
vector<vector<int>> t (n+1,vector<int> (m+1,-1) ) ;
int ans=LCS(s1,s2,n,m,t);
return ans;
}
};
//{ Driver Code Starts.
int32_t main()
{
int t; cin >> t;
while (t--)
{
string s; cin >> s;
Solution ob;
cout << ob.longestPalinSubseq(s) << endl;
}
}
// } Driver Code Ends