Skip to content

Commit

Permalink
Solved LeetCode Distinct Subsequence Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ghsatpute committed Apr 25, 2023
1 parent a7831cf commit cbea3cb
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
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()];
}
}
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
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);
}
}

0 comments on commit cbea3cb

Please sign in to comment.