-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ12_longestPalindromicSubsequence.cpp
More file actions
132 lines (111 loc) · 4.09 KB
/
Q12_longestPalindromicSubsequence.cpp
File metadata and controls
132 lines (111 loc) · 4.09 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
int byRecursion(string txt1 , string txt2 , int i , int j ){
//base case
if(i == txt1.length() || j == txt2.length()){
return 0;
}
//recursive call and logic
int ans = 0;
if(txt1[i] == txt2[j]){
ans = 1 + byRecursion(txt1, txt2 ,i+1 , j+1);
}
else{
ans = 0 + max(byRecursion(txt1 ,txt2 , i , j+1) , byRecursion(txt1, txt2 , i+1 , j));
}
return ans ;
}
///MARK HERE PASS THE STRIGS VIA REFERENCE TO AVOID TL ERROR
int byMemoisation(string& txt1 , string& txt2 , int i , int j, vector<vector<int> > dp){
//base case
if(i == txt1.length() || j == txt2.length()){
return 0;
}
// step3: checking if the answer is already present in dp
if(dp[i][j] != -1){
return dp[i][j];
}
//recursive call and logic
int ans = 0;
if(txt1[i] == txt2[j]){
ans = 1 + byMemoisation(txt1, txt2 ,i+1 , j+1 ,dp);
}
else{
ans = 0 + max(byMemoisation(txt1 ,txt2 , i , j+1,dp) , byMemoisation(txt1, txt2 , i+1 , j,dp));
}
//step2: storing the ans to the dp
dp[i][j] = ans;
return dp[i][j] ;
}
int byTabulation(string txt1, string txt2){
//step1 : creating the dp array(vector)
vector<vector<int> > dp(txt1.length()+1 , vector<int>(txt2.length()+1 , 0));
//no nneed for base cases because dp is initialized with 0 and base case is returning already 0 in the above case
//flow of the code
for(int i = txt1.length()-1 ; i>=0 ;i--){
for(int j =txt2.length() -1; j>=0 ;j--){
//logic
int ans = 0;
if(txt1[i] == txt2[j]){
ans = 1 + dp[i+1][j+1];
}
else{
ans = 0 + max( dp [i+1] [j] ,dp[i] [j+1]);
}
//step2: storing the ans to the dp
dp[i][j] = ans;
}
}
//ans is in [0][0] index because value of i and j in function parameter is i & j =0;
return dp[0][0] ;
}
int bySpaceOptimisation(string txt1, string txt2){
//creating 2 one dimentional vectors
vector<int>curr(txt2.length()+1 , 0);
vector<int>next(txt2.length()+1 , 0);
//no nneed for base cases because dp is initialized with 0 and base case is returning already 0 in the above case
//flow of the code
for(int i = txt1.length()-1 ; i>=0 ;i--){
for(int j =txt2.length() -1; j>=0 ;j--){
//logic
int ans = 0;
if(txt1[i] == txt2[j]){
ans = 1 + next[j+1];
}
else{
ans = 0 + max( curr [j+1] ,next [j]);
}
//step2: storing the ans to the dp
curr[j] = ans;
}
// shifting is improtant
next = curr;
}
//ans is in [0][0] index because value of i and j in function parameter is i & j =0;
return next[0] ;
}
int main(){
// string str = "bbbab";
string str = "cbbd";
//creating the ppalindrominc stings
string first = str;
reverse(str.begin(), str.end());
string second = str;
//two pointers approch
int i = 0;
int j = 0;
//the recursive solution
cout<<"the answer by recursion is : "<<byRecursion(first, second , i , j)<<endl;
//by topdown or memoisation approch
//step1: creating the vector
vector<vector<int> > dp(first.length()+1 , vector<int>(second.length()+1, -1));
cout<<"the answer by Memoisation is: "<<byMemoisation(first,second , i , j , dp)<<endl;
// by tabulation method
cout<<"the answer by tabulation method is: "<<byTabulation(first, second)<<endl;
//by space optimised solution
cout<<"tha answer by space optimised method is: "<<bySpaceOptimisation(first,second)<<endl;
return 0;
}