-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathEditdistance.cpp
102 lines (72 loc) · 1.49 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Name: Mehul Chaturvedi
IIT-Guwahati
*/
/*
Given two strings s and t of lengths m and n respectively, find the Edit Distance between the
strings. Edit Distance of two strings is minimum number of steps required to make one string
equal to other. In order to do so you can perform following three operations only :
1. Delete a character
2. Replace a character with another one
3. Insert a character
Note - Strings don't contain spaces
Input Format :
Line 1 : String s
Line 2 : String t
Output Format :
Line 1 : Edit Distance value
Constraints
1<= m,n <= 20
Sample Input 1 :
abc
dc
Sample Output 1 :
2
*/
#include <bits/stdc++.h>
using namespace std;
int editDistance(string s1, string s2){
int m = s1.size();
int n = s2.size();
vector<vector<int>>dp(m+1, vector<int>(n+1, 0));
for (int i = 0; i < m+1; ++i)
{
dp[i][n] = m-i;
}
for (int i = 0; i < n+1; ++i)
{
dp[m][i] = n-i;
}
//dp[m][n] = 0;
for (int i = m-1; i >= 0; --i)
{
for (int j = n-1; j >= 0; --j)
{
if(s1[i]==s2[j]){
dp[i][j] = dp[i+1][j+1];
}else{
dp[i][j] = min(1+dp[i+1][j+1], min(1+dp[i+1][j], 1+dp[i][j+1]));
}
}
}
// for (int i = 0; i < m+1; ++i)
// {
// for (int j = 0; j < n+1; ++j)
// {
// cout<<dp[i][j]<<" ";
// }
// cout << '\n';
// }
return dp[0][0];
}
int main( int argc , char ** argv )
{
ios_base::sync_with_stdio(false) ;
cin.tie(NULL) ;
string s1;
string s2;
cin >> s1;
cin >> s2;
cout << editDistance(s1,s2) << endl;
return 0 ;
}