-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum_Edit_Distance-DP.cpp
More file actions
55 lines (45 loc) · 1.26 KB
/
Copy pathMinimum_Edit_Distance-DP.cpp
File metadata and controls
55 lines (45 loc) · 1.26 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
#include <bits/stdc++.h>
using namespace std;
void minEditDistance(string a , string b)
{
int n = a.length();
int m = b.length();
int dp[n+1][m+1];
for(int i=0 ; i<=n ; i++)
{
for(int j=0 ; j<=m ; j++)
{
//string1 is empty, we can only insert
if(i == 0)
dp[i][j] = j;
//string2 is empty, we can only delete
else if(j == 0)
dp[i][j] = i;
else
{
//if character is same, no new operation is to be performed.
if(a[i-1] == b[j-1])
dp[i][j] = dp[i-1][j-1];
//if characters are different then find the minimum
else
{
int insertion = dp[i][j-1];
int deletion = dp[i-1][j];
int replacement = dp[i-1][j-1];
dp[i][j] = 1 + min(replacement , min(insertion , deletion));
}
}
}
}
cout<<"\nMinimum numbers of operations needed is : "<<dp[n][m]<<endl;
}
int main()
{
string a,b;
cout<<"Enter the first string : ";
cin>>a;
cout<<"Enter the first string : ";
cin>>b;
minEditDistance(a , b);
return 0;
}