-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditDistance.cpp
111 lines (94 loc) · 2.46 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
103
104
105
106
107
108
109
110
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cstdbool>
using namespace std;
int min(int x,int y,int z) {
return min(min(x, y), z);
}
int naiveEditDistance(string str1, string str2, int m, int n) {
if (m == 0)
return n;
if (n == 0)
return n;
if (str1[m-1] == str2[n-1])
return naiveEditDistance(str1, str2, (m-1), (n-1));
return 1 + min(naiveEditDistance(str1, str2, m-1, n),
naiveEditDistance(str1, str2, m, n-1),
naiveEditDistance(str1, str2, m-1, n-1)
);
}
int dpEditDistance(string str1, string str2) {
int m = str1.length();
int n = str2.length();
int **s = new int*[m+1];
for (int i = 0; i < (m+1) ; i++) {
s[i] = new int[n+1];
}
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0) {
s[i][j] = j;
} else if (j == 0) {
s[i][j] = i;
} else if (str1[i-1] == str2[j-1]) {
s[i][j] = s[i-1][j-1];
} else {
s[i][j] = 1 + min( s[i-1][j],
s[i][j-1],
s[i-1][j-1]
);
}
}
}
int editDist = s[m][n];
for (int i = 0; i < (m+1); i++) {
delete[] s[i];
}
delete[] s;
return editDist;
}
bool isSingleEdit(string str1, string str2) {
int m = str1.length();
int n = str2.length();
if (abs(m-n) > 1)
return false;
int i = 0;
int j = 0;
int count = 0;
while (i < m && j < n) {
if (str1[i] == str2[j]) {
i++;
j++;
} else {
if (count == 1)
return false;
count++;
if(m > n) {
i++;
} else if (n > m) {
j++;
} else {
i++;
j++;
}
}
}
if (i < m || j < n)
count++;
return (count == 1);
}
int main(int argc, char* argv[])
{
std::string str1, str2;
str1 = "sunday";
str2 = "saturday";
cout <<naiveEditDistance(str1, str2, str1.length(), str2.length()) << endl;
cout << dpEditDistance(str1, str2) << endl;
if (isSingleEdit("bus", "bust")) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}