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: ✅ Pattern 15: 0-1 Knapsack (Dynamic Programming).md
+79-4Lines changed: 79 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4522,13 +4522,13 @@ console.log(
4522
4522
// Output: 5
4523
4523
// Explanation: The longest common subsequence is "psspt".
4524
4524
```
4525
-
#### Bottom-up Dynamic Programming
4525
+
### Bottom-up Dynamic Programming
4526
4526
Since we want to match all the <b>subsequences</b> of the given two strings, we can use a two-dimensional array to store our results. The lengths of the two strings will define the size of the array’s two dimensions. So for every index `index1` in string `str1` and `index2` in string `str2`, we will choose one of the following two options:
4527
4527
4528
4528
1. If the character `str1[index1]` matches `str2[index2]`, the length of the <b>common subsequence</b> would be one plus the length of the <b>common subsequence</b> until the `index1-1` and `index2-1` indexes in the two respective strings.
4529
4529
2. If the character `str1[index1]`does not match `str2[index2]`, we will take the <i>longest subsequence</i> by either skipping `[index1]th` or `[index2]th` character from the respective strings.
4530
4530
4531
-
So our recursive formula would be:
4531
+
So our <b>recursive formula</b> would be:
4532
4532
```js
4533
4533
if str1[index1] == str2[index2]
4534
4534
dp[index1][index2] =1+ dp[index1-1][index2-1]
@@ -4582,10 +4582,85 @@ console.log(
4582
4582
### Challenge
4583
4583
Can we further improve our <b>bottom-up DP solution</b>? Can you find an algorithm that has `O(n)` <b>space complexity</b>?
4584
4584
4585
+
## Minimum Deletions & Insertions to Transform a String into another
> Given strings `str1` and `str2`, we need to transform `str1` into `str2` by deleting and inserting characters. Write a function to calculate the count of the minimum number of deletion and insertion operations.
4586
4589
4587
-
## Minimum Deletions & Insertions to Transform a String into another
4588
-
https://leetcode.com/problems/edit-distance/
4590
+
### Example 1:
4591
+
```js
4592
+
Input: str1 ="abc"
4593
+
str2 ="fbc"
4594
+
Output:1 deletion and 1 insertion.
4595
+
Explanation: We need to delete {'a'} and insert {'f'} to str1 to transform it into str2.
4596
+
```
4597
+
### Example 2:
4598
+
```js
4599
+
Input: str1 ="abdca"
4600
+
str2 ="cbda"
4601
+
Output:2 deletions and 1 insertion.
4602
+
Explanation: We need to delete {'a', 'c'} and insert {'c'} to str1 to transform it into str2.
4603
+
```
4604
+
### Example 3:
4605
+
```js
4606
+
Input: str1 ="passport"
4607
+
str2 ="ppsspt"
4608
+
Output:3 deletions and 1 insertion
4609
+
Explanation: We need to delete {'a', 'o', 'r'} and insert {'p'} to str1 to transform it into str2.
4610
+
```
4611
+
4612
+
This problem can easily be converted to the <b>[Longest Common Subsequence (LCS)](#🔎-longest-common-subsequence)</b>. If we can find the <b>LCS</b> of the two input strings, we can easily find how many characters we need to insert and delete from `str1`. Here is how we can do this:
4613
+
4614
+
1. Let’s assume `length1` is the length of `str1` and `length2` is the length of `str2`.
4615
+
2. Now let’s assume `c1` is the length of <b>LCS</b> of the two strings `str1` and `str2`.
4616
+
3. To transform `str1` into `str2`, we need to delete everything from `str1` which is not part of <b>LCS</b>, so minimum deletions we need to perform from `str1` => `length1 - c1`
4617
+
4. Similarly, we need to insert everything in `str1` which is present in `str2` but not part of <b>LCS</b>, so minimum insertions we need to perform in `str1` => `length2 - c1`
4618
+
4619
+
### Bottom-up Dynamic Programming Solution
4620
+
Let’s jump directly to the <b>bottom-up dynamic programming solution</b>:
4621
+
4622
+
```js
4623
+
functionfindMDI(str1, str2) {
4624
+
constc1=findLCSLength(str1, str2);
4625
+
4626
+
functionfindLCSLength(str1, str2) {
4627
+
constdp=Array(str1.length+1)
4628
+
.fill(0)
4629
+
.map(() =>Array(str2.length+1).fill(0));
4630
+
4631
+
let maxLength =0;
4632
+
4633
+
for (let i =1; i <=str1.length; i++) {
4634
+
for (let j =1; j <=str2.length; j++) {
4635
+
if (str1[i -1] === str2[j -1]) {
4636
+
dp[i][j] =1+ dp[i -1][j -1];
4637
+
} else {
4638
+
dp[i][j] =Math.max(dp[i -1][j], dp[i][j -1]);
4639
+
}
4640
+
maxLength =Math.max(maxLength, dp[i][j]);
4641
+
}
4642
+
}
4643
+
// console.log(dp);
4644
+
return maxLength;
4645
+
}
4646
+
4647
+
console.log(
4648
+
`We need ${str1.length- c1} deletions and ${str2.length- c1} insertions to transform "${str1}" into "${str2}"`);
4649
+
}
4650
+
4651
+
findMDI('abc', 'fbc');
4652
+
// Output: 1 deletion and 1 insertion.
4653
+
// Explanation: We need to delete {'a'} and insert {'f'} to s1 to transform it into s2.
4654
+
4655
+
findMDI('abdca', 'cbda');
4656
+
// Output: 2 deletions and 1 insertion.
4657
+
// Explanation: We need to delete {'a', 'c'} and insert {'c'} to s1 to transform it into s2.
4658
+
4659
+
findMDI('passport', 'ppsspt');
4660
+
// Output: 3 deletions and 1 insertion
4661
+
// Explanation: We need to delete {'a', 'o', 'r'} and insert {'p'} to s1 to transform it into s2.
4662
+
```
4663
+
- The <b>time and space complexity</b> of the above algorithm is `O(m*n)`, where `m` and `n` are the lengths of the two input strings.
0 commit comments