-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimum_edit_distance.cpp
54 lines (50 loc) · 1.11 KB
/
minimum_edit_distance.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
//============================================================================
//problem link :https://leetcode.com/problems/edit-distance/#/description
// Name : minimum_edit_distance.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//status : accepted
//============================================================================
#include <iostream>
#include<utility>
#include<string>
using namespace std;
int mini(int a,int b,int c){
if(a<b&&a<c)
return a;
else if(b<a&&b<c)
return b;
else
return c;
}
int get_min_edit_distace(string s1,string s2){
int m ,n;
m=s1.length();
n=s2.length();
int dMatrix[m+1][n+1];
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
if(i==0){
dMatrix[i][j]=j;
}
else if(j==0){
dMatrix[i][j]=i;
}
else if(s1[i-1]==s2[j-1])
{
dMatrix[i][j]=dMatrix[i-1][j-1];
}
else
dMatrix[i][j]=1+mini(dMatrix[i-1][j],dMatrix[i][j-1],dMatrix[i-1][j-1]);
}
}
return dMatrix[m][n];
}
int main() {
string s1,s2;
cin>>s1>>s2;
cout<<get_min_edit_distace(s1,s2);
return 0;
}