Skip to content

Commit 93c07b7

Browse files
profgrammerMadhavBahl
authored andcommitted
cpp code for day 7 and 8 (#98)
1 parent efb8f9d commit 93c07b7

File tree

4 files changed

+179
-3
lines changed

4 files changed

+179
-3
lines changed

day7/C++/profgrammer_oneedit.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
*@author: profgrammer
3+
*@date: 31-12-2018
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
#define inf 100000000
8+
using namespace std;
9+
10+
string s1, s2;
11+
12+
int dp[1500][1500];
13+
14+
// function that returns minimum edits required to convert s1[0 .. i-1] into s2[0 .. j-1]
15+
int minEdit(int i, int j){
16+
// base cases. if s1 finishes we need j insertions, if s2 finishes we need i insertions
17+
if(i == 0) return j;
18+
if(j == 0) return i;
19+
// housekeeping for dp with memoisation
20+
if(dp[i][j] != inf) return dp[i][j];
21+
// if the last characters are the same, no need to change anything and move both pointers by 1 unit
22+
if(s1[i-1] == s2[j-1]) {
23+
return dp[i][j] = minEdit(i-1, j-1);
24+
}
25+
// 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
26+
int minReplace = 1 + minEdit(i-1, j-1);
27+
// 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
28+
int minDelete = 1 + minEdit(i-1, j);
29+
// 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]
30+
int minInsert = 1 + minEdit(i, j-1);
31+
// return min of these 3 values
32+
return dp[i][j] = min(minReplace, min(minDelete, minInsert));
33+
}
34+
35+
int main() {
36+
for(int i = 0;i < 1500;i++){
37+
for(int j = 0;j < 1500;j++) dp[i][j] = inf;
38+
}
39+
cin>>s1>>s2;
40+
if(minEdit(s1.size(), s2.size()) <= 1) cout<<"The strings are 1 edit away\n";
41+
else cout<<"The strings are not 1 edit away\n";
42+
}

day7/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,52 @@ int main(){
498498
}
499499
```
500500
501+
### [C++ Solution](./C++/profgrammer_oneedit.cpp)
502+
```cpp
503+
/*
504+
*@author: profgrammer
505+
*@date: 31-12-2018
506+
*/
507+
508+
#include <bits/stdc++.h>
509+
#define inf 100000000
510+
using namespace std;
511+
512+
string s1, s2;
513+
514+
int dp[1500][1500];
515+
516+
// function that returns minimum edits required to convert s1[0 .. i-1] into s2[0 .. j-1]
517+
int minEdit(int i, int j){
518+
// base cases. if s1 finishes we need j insertions, if s2 finishes we need i insertions
519+
if(i == 0) return j;
520+
if(j == 0) return i;
521+
// housekeeping for dp with memoisation
522+
if(dp[i][j] != inf) return dp[i][j];
523+
// if the last characters are the same, no need to change anything and move both pointers by 1 unit
524+
if(s1[i-1] == s2[j-1]) {
525+
return dp[i][j] = minEdit(i-1, j-1);
526+
}
527+
// 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
528+
int minReplace = 1 + minEdit(i-1, j-1);
529+
// 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
530+
int minDelete = 1 + minEdit(i-1, j);
531+
// 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]
532+
int minInsert = 1 + minEdit(i, j-1);
533+
// return min of these 3 values
534+
return dp[i][j] = min(minReplace, min(minDelete, minInsert));
535+
}
536+
537+
int main() {
538+
for(int i = 0;i < 1500;i++){
539+
for(int j = 0;j < 1500;j++) dp[i][j] = inf;
540+
}
541+
cin>>s1>>s2;
542+
if(minEdit(s1.size(), s2.size()) <= 1) cout<<"The strings are 1 edit away\n";
543+
else cout<<"The strings are not 1 edit away\n";
544+
}
545+
```
546+
501547

502548
## C Implementation
503549

day8/Cpp/profgrammer_editdistance.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
*@author: profgrammer
3+
*@date: 31-12-2018
4+
*/
5+
6+
#include <bits/stdc++.h>
7+
#define inf 100000000
8+
using namespace std;
9+
10+
string s1, s2;
11+
12+
int dp[1500][1500];
13+
14+
// function that returns minimum edits required to convert s1[0 .. i-1] into s2[0 .. j-1]
15+
int minEdit(int i, int j){
16+
// base cases. if s1 finishes we need j insertions, if s2 finishes we need i insertions
17+
if(i == 0) return j;
18+
if(j == 0) return i;
19+
// housekeeping for dp with memoisation
20+
if(dp[i][j] != inf) return dp[i][j];
21+
// if the last characters are the same, no need to change anything and move both pointers by 1 unit
22+
if(s1[i-1] == s2[j-1]) {
23+
return dp[i][j] = minEdit(i-1, j-1);
24+
}
25+
// 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
26+
int minReplace = 1 + minEdit(i-1, j-1);
27+
// 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
28+
int minDelete = 1 + minEdit(i-1, j);
29+
// 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]
30+
int minInsert = 1 + minEdit(i, j-1);
31+
// return min of these 3 values
32+
return dp[i][j] = min(minReplace, min(minDelete, minInsert));
33+
}
34+
35+
int main() {
36+
for(int i = 0;i < 1500;i++){
37+
for(int j = 0;j < 1500;j++) dp[i][j] = inf;
38+
}
39+
cin>>s1>>s2;
40+
cout<<"The minimum edit distance is: ";
41+
cout<<minEdit(s1.size(), s2.size())<<endl;
42+
}

day8/README.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ console.log(minEditDist('kitten', 'sitting'));
101101

102102
## C++ Implementation
103103

104-
### [Solution 1](./Cpp/day8.cpp)
104+
### [Solution 1 by @dhruv-gupta14](./Cpp/day8.cpp)
105105

106106
```cpp
107107
/*
@@ -154,7 +154,53 @@ int main()
154154
}
155155
```
156156
157-
### [Solution 2 by @divyakhetan](./Cpp/EditDistanceday8.cpp)
157+
### [Solution 2 by @profgrammer](./Cpp/profgrammer_editdistance.cpp)
158+
```cpp
159+
/*
160+
*@author: profgrammer
161+
*@date: 31-12-2018
162+
*/
163+
164+
#include <bits/stdc++.h>
165+
#define inf 100000000
166+
using namespace std;
167+
168+
string s1, s2;
169+
170+
int dp[1500][1500];
171+
172+
// function that returns minimum edits required to convert s1[0 .. i-1] into s2[0 .. j-1]
173+
int minEdit(int i, int j){
174+
// base cases. if s1 finishes we need j insertions, if s2 finishes we need i insertions
175+
if(i == 0) return j;
176+
if(j == 0) return i;
177+
// housekeeping for dp with memoisation
178+
if(dp[i][j] != inf) return dp[i][j];
179+
// if the last characters are the same, no need to change anything and move both pointers by 1 unit
180+
if(s1[i-1] == s2[j-1]) {
181+
return dp[i][j] = minEdit(i-1, j-1);
182+
}
183+
// 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
184+
int minReplace = 1 + minEdit(i-1, j-1);
185+
// 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
186+
int minDelete = 1 + minEdit(i-1, j);
187+
// 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]
188+
int minInsert = 1 + minEdit(i, j-1);
189+
// return min of these 3 values
190+
return dp[i][j] = min(minReplace, min(minDelete, minInsert));
191+
}
192+
193+
int main() {
194+
for(int i = 0;i < 1500;i++){
195+
for(int j = 0;j < 1500;j++) dp[i][j] = inf;
196+
}
197+
cin>>s1>>s2;
198+
cout<<"The minimum edit distance is: ";
199+
cout<<minEdit(s1.size(), s2.size())<<endl;
200+
}
201+
```
202+
203+
### [Solution 3 by @divyakhetan](./Cpp/EditDistanceday8.cpp)
158204

159205
```cpp
160206
/**
@@ -200,7 +246,7 @@ int main(){
200246
}
201247
```
202248
203-
### [Solution 3 by @aaditkamat] (./C++/levenshtein_distance.cpp)
249+
### [Solution 4 by @aaditkamat] (./C++/levenshtein_distance.cpp)
204250
205251
```cpp
206252
/**

0 commit comments

Comments
 (0)