-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solved LeetCode Distinct Subsequence Problem
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...rithm/dynamicProgramming/distinctSubsequneces/DistinctSubsequencesDynamicProgramming.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package problemsolving.leetcode.algorithm.dynamicProgramming.distinctSubsequneces; | ||
|
||
public class DistinctSubsequencesDynamicProgramming { | ||
public int numDistinct(String s, String t) { | ||
int[][] dp = new int[s.length() + 1][t.length() + 1]; | ||
for (int i = 0; i < s.length() + 1; i++) { | ||
// Because empty target string always matches | ||
dp[i][0] = 1; | ||
} | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
for (int j = 0; j < t.length(); j++) { | ||
if (s.charAt(i) == t.charAt(j)) { | ||
// If current characters are match, then we consider left-top cell | ||
// as well as left cell and add that to current output | ||
dp[i + 1][j + 1] = dp[i][j + 1] + dp[i][j]; | ||
} else { | ||
// If current character doesn't match, we can just consider only left cell as it is | ||
dp[i + 1][j + 1] = dp[i][j + 1]; | ||
} | ||
} | ||
} | ||
|
||
return dp[s.length()][t.length()]; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...lemsolving/leetcode/algorithm/dynamicProgramming/distinctSubsequneces/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# 115. Distinct Subsequences | ||
|
||
Given two strings `s` and `t`, return the number of distinct subsequences | ||
of `s` which equals `t`. | ||
|
||
The test cases are generated so that the answer fits on a 32-bit signed integer. | ||
|
||
### Example 1 | ||
``` | ||
Input: s = "rabbbit", t = "rabbit" | ||
Output: 3 | ||
Explanation: | ||
As shown below, there are 3 ways you can generate "rabbit" from s. | ||
rabb ~b~ it | ||
rab ~b~ bit | ||
ra ~b~ bbit | ||
String surrounding ~ is not taken into consideration | ||
``` | ||
|
||
### Example 2 | ||
``` | ||
Input: s = "babgbag", t = "bag" | ||
Output: 5 | ||
Explanation: | ||
As shown below, there are 5 ways you can generate "bag" from s. | ||
ba ~b~ g ~bag~ | ||
ba ~bgba~ g | ||
b ~abgb~ ag | ||
~ba~ b ~gb~ ag | ||
~babg~ bag | ||
String surrounding ~ is not taken into consideration | ||
``` | ||
|
||
## Constraints | ||
* `1 <= s.length, t.length <= 1000` | ||
* `s` and `t` consist of English letters. | ||
|
||
# Solution Reference | ||
https://www.youtube.com/watch?v=-RDzMJ33nx8&ab_channel=NeetCode | ||
Not exactly followed above solution. Instead of using map, used 2D array and solved the solution |
27 changes: 27 additions & 0 deletions
27
...m/dynamicProgramming/distinctSubsequneces/DistinctSubsequencesDynamicProgrammingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package problemsolving.leetcode.algorithm.dynamicProgramming.distinctSubsequneces; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class DistinctSubsequencesDynamicProgrammingTest { | ||
@Test | ||
public void testCase01() { | ||
String source = "rabbbit"; | ||
String target = "rabbit"; | ||
|
||
int output = new DistinctSubsequencesDynamicProgramming().numDistinct(source, target); | ||
|
||
assertEquals(3, output); | ||
} | ||
|
||
@Test | ||
public void testCase02() { | ||
String source = "babgbag"; | ||
String target = "bag"; | ||
|
||
int output = new DistinctSubsequencesDynamicProgramming().numDistinct(source, target); | ||
|
||
assertEquals(5, output); | ||
} | ||
} |