Skip to content

Commit 74d53d4

Browse files
committed
Update 0072-edit-distance.md
1 parent 5561157 commit 74d53d4

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

dsa-solutions/lc-solutions/0000-0099/0072-edit-distance.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,48 +65,38 @@ For every index of string S1, we have three options to match that index with str
6565
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.
6666

6767
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.
7070
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.
7272

7373
#### Implementation
7474

7575
```C++
7676
int editDistanceUtil(string& S1, string& S2, int i, int j, vector<vector<int>>& dp) {
77-
// Base cases
7877
if (i < 0)
7978
return j + 1;
8079
if (j < 0)
8180
return i + 1;
8281

83-
// If the result for this state has already been calculated, return it
8482
if (dp[i][j] != -1)
8583
return dp[i][j];
8684

87-
// If the characters at the current positions match, no operation is needed
8885
if (S1[i] == S2[j])
8986
return dp[i][j] = 0 + editDistanceUtil(S1, S2, i - 1, j - 1, dp);
9087

91-
// Minimum of three choices:
92-
// 1. Replace the character at S1[i] with the character at S2[j]
93-
// 2. Delete the character at S1[i]
94-
// 3. Insert the character at S2[j] into S1
9588
else
9689
return dp[i][j] = 1 + min(editDistanceUtil(S1, S2, i - 1, j - 1, dp),
9790
min(editDistanceUtil(S1, S2, i - 1, j, dp),
9891
editDistanceUtil(S1, S2, i, j - 1, dp)));
9992
}
10093

101-
// Function to calculate the minimum number of operations required to transform S1 into S2
10294
int editDistance(string& S1, string& S2) {
10395
int n = S1.size();
10496
int m = S2.size();
10597

106-
// Create a DP table to memoize results
10798
vector<vector<int>> dp(n, vector<int>(m, -1));
10899

109-
// Call the utility function with the last indices of both strings
110100
return editDistanceUtil(S1, S2, n - 1, m - 1, dp);
111101
}
112102
```

0 commit comments

Comments
 (0)