You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* cpp code for day 7 and 8
* cpp code for day 7 and 8
* fixed readme merge conflict
* added cpp code for day 38
Co-authored-by: MADHAV BAHL <madhavbahl10@gmail.com>
Copy file name to clipboardExpand all lines: day8/README.md
+51Lines changed: 51 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -200,6 +200,55 @@ int main() {
200
200
}
201
201
```
202
202
203
+
204
+
=======
205
+
### [Solution 2 by @profgrammer](./Cpp/profgrammer_editdistance.cpp)
206
+
```cpp
207
+
/*
208
+
*@author: profgrammer
209
+
*@date: 31-12-2018
210
+
*/
211
+
212
+
#include<bits/stdc++.h>
213
+
#defineinf 100000000
214
+
using namespace std;
215
+
216
+
string s1, s2;
217
+
218
+
int dp[1500][1500];
219
+
220
+
// function that returns minimum edits required to convert s1[0 .. i-1] into s2[0 .. j-1]
221
+
intminEdit(int i, int j){
222
+
// base cases. if s1 finishes we need j insertions, if s2 finishes we need i insertions
223
+
if(i == 0) return j;
224
+
if(j == 0) return i;
225
+
// housekeeping for dp with memoisation
226
+
if(dp[i][j] != inf) return dp[i][j];
227
+
// if the last characters are the same, no need to change anything and move both pointers by 1 unit
228
+
if(s1[i-1] == s2[j-1]) {
229
+
return dp[i][j] = minEdit(i-1, j-1);
230
+
}
231
+
// replace s1[i] to be s2[j]. the strings to be checked are still s1[0 .. i-1] and s2[0 .. j-1] but the cost is +1
232
+
int minReplace = 1 + minEdit(i-1, j-1);
233
+
// delete s1[i], cost is +1 and the strings are now s1[0 .. i-1] and s2[0 .. j] because we need to compare the i-1th character with the jth character
234
+
int minDelete = 1 + minEdit(i-1, j);
235
+
// insert a character into s1 at index i. cost is +1 and now the strings to be checked are s1[0 .. i] because the ith character isn't compared yet and s2[0 .. j-1]
0 commit comments