-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditDistance.cpp
60 lines (55 loc) · 1.35 KB
/
editDistance.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
55
56
57
58
59
60
//============================================================================
//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
//============================================================================
/*
* note this solution using dynamic programming technique .
*
*
* */
#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 word1,string word2)
{
int m ,n;
m=word1.length();//m is the length of the first string
n=word2.length();//n is the length of the second string
int dMatrix[m+1][n+1];//the matrix
for(int i=0;i<=m;i++){//initialize the first column
dMatrix[i][0]=i;
}
for(int i=0;i<=n;i++){//initialize the first row
dMatrix[0][i]=i;
}
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++){
if(word1[i-1]==word2[j-1])
dMatrix[i][j]=dMatrix[i-1][j-1];
else
dMatrix[i][j]=mini(dMatrix[i-1][j],dMatrix[i][j-1],dMatrix[i-1][j-1])+1;
}
}
return dMatrix[m][n];
}
int main() {
string word1,word2;
cin>>word1>>word2;
cout<<get_min_edit_distace(word1,word2);
return 0;
}