-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path72. Edit Distance.cpp
More file actions
218 lines (194 loc) · 6.94 KB
/
72. Edit Distance.cpp
File metadata and controls
218 lines (194 loc) · 6.94 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
class Solution {
public:
int minDistance(string word1, string word2) {
int n=word1.length();
int m=word2.length();
int dp[n+1][m+1];
for(int i=0;i<=n;i++){
for(int j=0;j<=m;j++){
if(i==0)
dp[i][j]=j;
else if(j==0)
dp[i][j]=i;
else if(word1[i-1]==word2[j-1])
dp[i][j]=dp[i-1][j-1];
else
dp[i][j]=1+min(dp[i-1][j],min(dp[i][j-1],dp[i-1][j-1]));
}
}
return dp[n][m];
}
};
/*another approach*/
//DP
//Runtime: 32 ms, faster than 17.80% of C++ online submissions for Edit Distance.
//Memory Usage: 8.9 MB, less than 87.50% of C++ online submissions for Edit Distance.
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.size();
int n = word2.size();
/*
different from other problem,
here dp[0][?] and dp[?][0] are meaningful.
They represents the situation when word1 is "" and when word2 is ""
*/
vector<vector<int>> dp(m+1, vector(n+1, 0));
for(int i = 0; i <= m; i++){
for(int j = 0; j <= n; j++){
if(i == 0 && j == 0){
dp[i][j] = 0;
}else if(j == 0){
/*
word1: [0, i-1] //i 's upper limit is m
word2: ""
this means we need to delete i char from word1
*/
dp[i][j] = i;
}else if(i == 0){
/*
word1: ""
word2: [0, j-1] //j 's upper limit is n
*/
dp[i][j] = j;
}else if(word1[i-1] == word2[j-1]){
//no op
dp[i][j] = dp[i-1][j-1];
}else{
dp[i][j] = min({
//remove word1[i-1]
dp[i-1][j],
//insert word2[j-1]
dp[i][j-1],
//replace word1[i-1] to word2[j-1]
dp[i-1][j-1]
}) + 1;
}
}
}
return dp[m][n];
}
};
//DP, O(1) space
//https://leetcode.com/problems/edit-distance/discuss/25846/C%2B%2B-O(n)-space-DP
//Runtime: 16 ms, faster than 61.68% of C++ online submissions for Edit Distance.
//Memory Usage: 6.3 MB, less than 100.00% of C++ online submissions for Edit Distance.
class Solution {
public:
int minDistance(string word1, string word2) {
int m = word1.size();
int n = word2.size();
vector<int> dp(n+1, 0);
int dp_is1_js1;
for(int i = 0; i <= m; i++){
for(int j = 0; j <= n; j++){
int tmp = dp[j];
if(i == 0 && j == 0){
dp[j] = 0;
}else if(j == 0){
/*
word1: [0, i-1] //i 's upper limit is m
word2: ""
this means we need to delete i char from word1
*/
dp[j] = i;
}else if(i == 0){
/*
word1: ""
word2: [0, j-1] //j 's upper limit is n
*/
dp[j] = j;
}else if(word1[i-1] == word2[j-1]){
//no op
dp[j] = dp_is1_js1;
}else{
dp[j] = min({
//remove word1[i-1]
//dp[i-1][j]
dp[j],
//insert word2[j-1]
//dp[i][j-1]
dp[j-1],
//replace word1[i-1] to word2[j-1]
//dp[i-1][j-1]
dp_is1_js1
}) + 1;
}
dp_is1_js1 = tmp;
}
}
return dp[n];
}
};
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
int min(int x, int y, int z) {
return std::min(std::min(x, y), z);
}
int edit_distance(string s1, string s2, int m, int n) {
// if first string is empty, only option is insert all chars in s2.
if (m == 0) return n;
// if second string is empty, only option is remove all chars in s1.
if (n == 0) return m;
// if the last chars are equal, ignore them, remove the distance for
// remaining strings
if (s1[m-1] == s2[n-1]) {
return edit_distance(s1, s2, m - 1, n - 1);
}
// If last characters are not same, consider all three
// operations on last character of first string, recursively
// compute minimum cost for all three operations and take
// minimum of three values.
return 1 + min(edit_distance(s1, s2, m, n - 1), // insert, so s1[m+1] == s2[n]
edit_distance(s1, s2, m - 1, n), // remove
edit_distance(s1, s2, m - 1, n - 1) // replace
);
}
public:
// return the minimum number of operations to convert s1 to s2
int edit_distance(string s1, string s2) {
return edit_distance(s1, s2, s1.length(), s2.length());
}
int edit_distance_dp(string s1, string s2) {
int m = s1.length(), n = s2.length();
vector< vector<int> > dp(m+1, vector<int>(n+1, 0));
for (int i = 0; i < m + 1; ++i) {
for (int j = 0; j < n + 1; ++j) {
if (i == 0) {
// if no chars in s1, insert all chars of s2
dp[i][j] = j;
}
else if (j == 0) {
// if no chars in s2, remove all chars in s1
dp[i][j] = i;
}
else if (s1[i-1] == s2[j-1]) {
// if last chars are same, ignore them and recur for remaining
dp[i][j] = dp[i-1][j-1];
}
else {
// if not same, consider all possibilities and find min
dp[i][j] = 1 + min(dp[i][j-1], // insert
dp[i-1][j], // remove
dp[i-1][j-1] // replace
);
}
}
}
return dp[m][n];
}
};
int main() {
string str1 = "sunday";
string str2 = "saturday";
Solution sol;
cout << "The edit distance between " << str1 << " and " << str2 << " is " <<
sol.edit_distance( str1 , str2) << endl;
cout << "The edit distance between " << str1 << " and " << str2 << " is " <<
sol.edit_distance_dp( str1 , str2) << endl;
return 0;
}