-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumInsOrDeletion.cpp
More file actions
44 lines (39 loc) · 1.1 KB
/
MinimumInsOrDeletion.cpp
File metadata and controls
44 lines (39 loc) · 1.1 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
//Problem statement-Given two strings ‘str1’ and ‘str2’ of size m and n respectively. The task is to remove/delete and insert the minimum number of characters from/in str1 to transform it into str2. It could be possible that the same character needs to be removed/deleted from one point of str1 and inserted to some another point.
#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 = "GEEKSFORGEEKS";
string Y = "GEEKS";
int m=X.length();
int n=Y.length();
int t[m][maximum];
memset(t,-1,sizeof(t));
int lcs=LCS(X,Y,m,n,t);
cout<<"No.of deletions : "<<m-lcs<<endl;
cout<<"No.of insertions: "<<n-lcs;
return 0;
}