-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ13_EditDistanceProblem.cpp
More file actions
187 lines (160 loc) · 5.88 KB
/
Q13_EditDistanceProblem.cpp
File metadata and controls
187 lines (160 loc) · 5.88 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
#include<iostream>
#include<vector>
using namespace std;
int solveByRecursion(string txt1 ,string txt2, int i , int j){
//base cases
if( i == txt1.length()){
return txt2.length() - j ;
}
if( j == txt2.length()){
return txt1.length() - i;
}
//initializing the ans
int ans = 0;
//logic
if(txt1[i] == txt2[j]){
ans = solveByRecursion(txt1, txt2, i+1 , j+1);
}
else{
// one operation cost 1 unit
int insert = 1+ solveByRecursion(txt1, txt2, i ,j+1);
int deleted = 1+ solveByRecursion(txt1, txt2, i+1, j );
int replaced = 1+ solveByRecursion(txt1, txt2 ,i+1 , j+1);
//we have to give minimum ways (units) ans thats why returning the min of all operations
ans = min(insert , min(deleted , replaced));
}
return ans;
}
int soleByMemoisation(string& txt1 ,string& txt2, int i , int j,vector<vector<int> > &dp ){
//base cases
if( i == txt1.length()){
return txt2.length() - j ;
}
if( j == txt2.length()){
return txt1.length() - i;
}
// step3 : cheking if the ANS IS ALREADY EXIST
if(dp[i][j] != -1){
return dp[i][j];
}
//initializing the ans
int ans = 0;
//logic
if(txt1[i] == txt2[j]){
ans = soleByMemoisation(txt1, txt2, i+1 , j+1,dp);
}
else{
// one operation cost 1 unit
int insert = 1+ soleByMemoisation(txt1, txt2, i ,j+1,dp );
int deleted = 1+ soleByMemoisation(txt1, txt2, i+1, j ,dp );
int replaced = 1+ soleByMemoisation(txt1, txt2 ,i+1 , j+1 ,dp);
//we have to give minimum ways (units) ans thats why returning the min of all operations
ans = min(insert , min(deleted , replaced));
}
// step2: storing the ans to the dp arrat
dp[i][j] = ans;
return dp[i][j];
}
int solveByTabulation(string txt1, string txt2){
//step1 : creating the dpAray
vector<vector<int> > dp(txt1.length()+1 , vector<int>(txt2.length()+1 ,0));
//analizing the base cass
for(int j = 0 ;j<= txt2.length() ; j++){
dp[txt1.length()][j] = txt2.length() -j;
}
for(int i =0; i<= txt1.length();i++){
dp[i][txt2.length()] = txt1.length() -i;
}
//settting up the flow of the code
// NOT FORGET TO START THE i FROM .length()-1
for(int i = txt1.length()-1 ; i>=0;i--){
for(int j = txt2.length()-1 ; j>=0 ;j--){
//logic
//initializing the ans
int ans = 0;
//logic
if(txt1[i] == txt2[j]){
ans = dp[ i+1] [ j+1];
}
else{
// one operation cost 1 unit
int insert = 1+ dp[ i ][j+1];
int deleted = 1+ dp[ i+1][ j];
int replaced = 1+ dp[i+1] [ j+1];
//we have to give minimum ways (units) ans thats why returning the min of all operations
ans = min(insert , min(deleted , replaced));
}
// step2: storing the ans to the dp arrat
dp[i][j] = ans;
}
}
return dp[0][0];
}
int SpaceOptimisedSolution(string txt1, string txt2){
//by two one dimentional dparrays
vector<int>curr(txt2.length()+1 , 0);
vector<int>next(txt2.length()+1 , 0);
//GIVE SPECIAL ATTENTION ON THE BASE CASES
for(int j = 0 ;j<= txt2.length() ; j++){
next[j] = txt2.length() -j;
}
//settting up the flow of the code
// NOT FORGET TO START THE i FROM .length()-1
for(int i = txt1.length()-1 ; i>=0;i--){
//every row starts here
curr[txt2.length()] = txt1.length()-i;
for(int j = txt2.length()-1 ; j>=0 ;j--){
//logic
//initializing the ans
int ans = 0;
//logic
if(txt1[i] == txt2[j]){
ans = next[ j+1];
}
else{
// one operation cost 1 unit
int insert = 1+ curr[j+1];
int deleted = 1+ next[ j];
int replaced = 1+ next [ j+1];
//we have to give minimum ways (units) ans thats why returning the min of all operations
ans = min(insert , min(deleted , replaced));
}
// step2: storing the ans to the dp arrat
curr[j] = ans;
}
//SHIFTING IS IMPORTANT
next = curr;
}
//returning the answer
return next[0];
}
int main(){
// string word1 ="horse";
// string word2 ="ros";
string word1 ="intention";
string word2 ="execution";
//solution
if(word1.length() ==0){
return word2.length();
}
if(word2.length() == 0){
return word1.length();
}
int i = 0 ;
int j = 0;
//methhod 1
int ansByRec = solveByRecursion(word1 ,word2, i , j);
cout<<"The answer of recursive solution is: "<<ansByRec<<endl;
//method 2: solving by memoisation method
//step1: creating the dp array (vector)
vector<vector<int> > dp(word1.length()+1 , vector<int>(word2.length() , -1));
int ansByMemoisation = soleByMemoisation(word1, word2, i , j , dp);
cout<<"The answer of Memoisation solution is: "<<ansByMemoisation<<endl;
//method 3: the tabulaton method
int ansByTabulation = solveByTabulation(word1, word2);
cout<<"the answer of tabulaton solution is : "<<ansByTabulation<<endl;
//Method 4: solving by space optimised
int ansBySpaceOptimisation = SpaceOptimisedSolution(word1, word2);
cout<<"the answer of space optimised solution is : "<< ansByMemoisation<<endl;
return 0;
}