forked from akkupy/codeDump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditDistance.cpp
135 lines (106 loc) · 3.8 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// https://leetcode.com/problems/edit-distance/description/
class Solution {
public:
// Simple Recursive Solution
// int solve(string& s1, string& s2, int i, int j){
// if(i==s1.length())
// return s2.length()-j;
// if(j==s2.length())
// return s1.length()-i;
// int ans=0;
// if(s1[i]==s2[j]){
// ans= solve(s1, s2, i+1, j+1);
// }
// else{
// int insertAns= 1 + solve(s1, s2, i, j+1);
// int deleteAns= 1 + solve(s1, s2, i+1, j);
// int replaceAns= 1 + solve(s1, s2, i+1, j+1);
// ans= min(insertAns, min(deleteAns, replaceAns));
// }
// return ans;
// }
// Recursion + Memoization (Top-Down Approach)
// int solve(string& s1, string& s2, int i, int j, vector<vector<int>>& dp){
// if(i==s1.length())
// return s2.length()-j;
// if(j==s2.length())
// return s1.length()-i;
// if(dp[i][j]!=-1)
// return dp[i][j];
// int ans=0;
// if(s1[i]==s2[j]){
// ans= solve(s1, s2, i+1, j+1, dp);
// }
// else{
// int insertAns= 1 + solve(s1, s2, i, j+1, dp);
// int deleteAns= 1 + solve(s1, s2, i+1, j, dp);
// int replaceAns= 1 + solve(s1, s2, i+1, j+1, dp);
// ans= min(insertAns, min(deleteAns, replaceAns));
// }
// dp[i][j]= ans;
// return dp[i][j];
// }
// Tabulation Method (Bottom-Up Approach)
// int solve(string& s1, string& s2){
// vector<vector<int>> dp(s1.length()+1, vector<int>(s2.length()+1, 0));
// for(int i=0; i<s2.length(); i++){
// dp[s1.length()][i]= s2.length()-i;
// }
// for(int i=0; i<s1.length(); i++){
// dp[i][s2.length()]= s1.length()-i;
// }
// for(int i= s1.length()-1; i>=0; i--){
// for(int j= s2.length()-1; j>=0; j--){
// int ans=0;
// if(s1[i]==s2[j]){
// ans= dp[i+1][j+1];
// }
// else{
// int insertAns= 1 + dp[i][j+1];
// int deleteAns= 1 + dp[i+1][j];
// int replaceAns= 1 + dp[i+1][j+1];
// ans= min(insertAns, min(deleteAns, replaceAns));
// }
// dp[i][j]= ans;
// }
// }
// return dp[0][0];
// }
int solve(string& s1, string& s2){
vector<int> curr(s2.length()+1, 0);
vector<int> next(s2.length()+1, 0);
for(int i=0; i<s2.length(); i++){
next[i]= s2.length()-i;
}
for(int i= s1.length()-1; i>=0; i--){
for(int j= s2.length()-1; j>=0; j--){
curr[s2.length()]= s1.length()-i;
int ans=0;
if(s1[i]==s2[j]){
ans= next[j+1];
}
else{
int insertAns= 1 + curr[j+1];
int deleteAns= 1 + next[j];
int replaceAns= 1 + next[j+1];
ans= min(insertAns, min(deleteAns, replaceAns));
}
curr[j]= ans;
}
next= curr;
}
return curr[0];
}
int minDistance(string word1, string word2) {
// Simple Recursive Solution
// return solve(word1, word2, 0, 0);
// Recursion + Memoization (Top-Down Approach)
// vector<vector<int>> dp(word1.length(), vector<int>(word2.length(), -1));
// return solve(word1, word2, 0, 0, dp);
if(word1.length()==0)
return word2.length();
if(word2.length()==0)
return word1.length();
return solve(word1, word2);
}
};