-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinDeletionToPalindrome.cpp
More file actions
40 lines (38 loc) · 1.01 KB
/
MinDeletionToPalindrome.cpp
File metadata and controls
40 lines (38 loc) · 1.01 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
//Given a string of size ‘n’. The task is to remove or delete the minimum number of characters from the string so that the resultant string is a palindrome.
//Note: The order of characters should be maintained.
#include<bits/stdc++.h>
using namespace std;
const int maximum=1000;
int LCS(string X,string Y,int m,int n,int t[][maximum])
{
for(int i=0;i<=m;i++)
{
for(int j=0;j<=n;j++)
{
if(i==0|| j==0)
t[i][j]=0;
}
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(X[i-1]==Y[j-1])
t[i][j]=t[i-1][j-1]+1;
else
t[i][j]=max(t[i-1][j],t[i][j-1]);
}
}
return t[m][n];
}
int main()
{
string X = "AGBCBA";
string Y = "ABCBGA";
int m=X.length();
int n=Y.length();
int t[m][maximum];
memset(t,-1,sizeof(t));
cout<<"Longest Palindromic sequence: "<<(m-LCS(X,Y,m,n,t));//given string sai agr length of LCS subtract krdein then we get the minimum no. of deletions.
return 0;
}