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
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0000-0099/0072-edit-distance.md
+3-13Lines changed: 3 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -65,48 +65,38 @@ For every index of string S1, we have three options to match that index with str
65
65
As there is no uniformity in data, there is no other way to find out than to try out all possible ways. To do so we will need to use recursion.
66
66
67
67
Steps to memoize a recursize solution:
68
-
* Create a dp array of size [n][m]. The size of S1 and S2 are n and m respectively, so the variable i will always lie between ‘0’ and ‘n-1’ and the variable j between ‘0’ and ‘m-1’.
69
-
** We initialize the dp array to -1.
68
+
- Create a dp array of size [n][m]. The size of S1 and S2 are n and m respectively, so the variable i will always lie between ‘0’ and ‘n-1’ and the variable j between ‘0’ and ‘m-1’.
69
+
- We initialize the dp array to -1.
70
70
Whenever we want to find the answer to particular parameters (say f(i,j)), we first check whether the answer is already calculated using the dp array(i.e dp[i][j]!= -1 ). If yes, simply return the value from the dp array.
71
-
* If not, then we are finding the answer for the given value for the first time, we will use the recursive relation as usual but before returning from the function, we will set dp[i][j] to the solution we get.
71
+
- If not, then we are finding the answer for the given value for the first time, we will use the recursive relation as usual but before returning from the function, we will set dp[i][j] to the solution we get.
72
72
73
73
#### Implementation
74
74
75
75
```C++
76
76
inteditDistanceUtil(string& S1, string& S2, int i, int j, vector<vector<int>>& dp) {
77
-
// Base cases
78
77
if (i < 0)
79
78
return j + 1;
80
79
if (j < 0)
81
80
return i + 1;
82
81
83
-
// If the result for this state has already been calculated, return it
84
82
if (dp[i][j] != -1)
85
83
return dp[i][j];
86
84
87
-
// If the characters at the current positions match, no operation is needed
0 commit comments