forked from Ayushsinhahaha/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdit Distance.cpp
More file actions
39 lines (26 loc) · 708 Bytes
/
Edit Distance.cpp
File metadata and controls
39 lines (26 loc) · 708 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int editDistance(string& S1, string& S2){
int n = S1.size();
int m = S2.size();
vector<int> prev(m+1,0);
vector<int> cur(m+1,0);
for(int j=0;j<=m;j++){
prev[j] = j;
}
for(int i=1;i<n+1;i++){
cur[0]=i;
for(int j=1;j<m+1;j++){
if(S1[i-1]==S2[j-1])
cur[j] = 0+prev[j-1];
else cur[j] = 1+min(prev[j-1],min(prev[j],cur[j-1]));
}
prev = cur;
}
return prev[m];
}
int main() {
string s1 = "horse";
string s2 = "ros";
cout << "The minimum number of operations required is: "<<editDistance(s1,s2);
}