-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0072-edit-distance.cpp
132 lines (100 loc) · 3.63 KB
/
0072-edit-distance.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class Solution {
public:
int solveRec(string str1, string str2, int i, int j) {
if(i == str1.length()) {
return str2.length() - j;
}
if(j == str2.length()) {
return str1.length() - i;
}
int ans = 0;
if(str1[i] == str2[j]) {
return solveRec(str1, str2, i+1, j+1);
} else {
int insAns = 1 + solveRec(str1, str2, i, j+1);
int delAns = 1 + solveRec(str1, str2, i+1, j);
int repAns = 1 + solveRec(str1, str2, i+1, j+1);
ans = min(insAns, min(delAns, repAns));
}
return ans;
}
int solveMem(vector<vector<int>> &dp, string str1, string str2, int i, int j) {
if(i == str1.length()) {
return str2.length() - j;
}
if(j == str2.length()) {
return str1.length() - i;
}
if(dp[i][j] != -1) {
return dp[i][j];
}
int ans = 0;
if(str1[i] == str2[j]) {
return solveMem(dp, str1, str2, i+1, j+1);
} else {
int insAns = 1 + solveMem(dp, str1, str2, i, j+1);
int delAns = 1 + solveMem(dp, str1, str2, i+1, j);
int repAns = 1 + solveMem(dp, str1, str2, i+1, j+1);
ans = min(insAns, min(delAns, repAns));
}
dp[i][j] = ans;
return dp[i][j];
}
int solveTab(string str1, string str2, int len1, int len2) {
vector<vector<int>> dp(len1 + 1, vector<int>(len2 + 1, 0));
for(int j=0; j<len2; j++) {
dp[len1][j] = len2 - j;
}
for(int i=0; i<len1; i++) {
dp[i][len2] = len1 - i;
}
for(int i=len1-1; i>=0; i--) {
for(int j=len2-1; j>=0; j--) {
int ans = 0;
if(str1[i] == str2[j]) {
ans = dp[i+1][j+1];
} else {
int insAns = 1 + dp[i][j+1];
int delAns = 1 + dp[i+1][j];
int repAns = 1 + dp[i+1][j+1];
ans = min(insAns, min(delAns, repAns));
}
dp[i][j] = ans;
}
}
return dp[0][0];
}
int solveSpc(string str1, string str2, int len1, int len2) {
if(len1 == 0) return len2;
if(len2 == 0) return len1;
vector<int> curr(len2+1, 0);
vector<int> next(len2+1, 0);
for(int j=0; j<len2; j++) {
next[j] = len2 - j;
}
for(int i=len1-1; i>=0; i--) {
for(int j=len2-1; j>=0; j--) {
curr[len2] = len1 - i;
int ans = 0;
if(str1[i] == str2[j]) {
ans = next[j+1];
} else {
int insAns = 1 + curr[j+1];
int delAns = 1 + next[j];
int repAns = 1 + next[j+1];
ans = min(insAns, min(delAns, repAns));
}
curr[j] = ans;
}
next = curr;
}
return next[0];
}
int minDistance(string word1, string word2) {
// return solveRec(word1, word2, 0, 0);
// vector<vector<int>> dp(word1.length(), vector<int>(word2.length(), -1));
// return solveMem(dp, word1, word2, 0, 0);
// return solveTab(word1, word2, word1.length(), word2.length());
return solveSpc(word1, word2, word1.length(), word2.length());
}
};