Skip to content

Commit

Permalink
solution(cpp,java): 115. Distinct Subsequences
Browse files Browse the repository at this point in the history
115. Distinct Subsequences
- C++
- Java
  • Loading branch information
godkingjay authored Oct 31, 2023
2 parents c8f0c86 + f91883e commit c415a9d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Hard/115. Distinct Subsequences/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Distinct Subsequences Problem Explained :sparkles:

## Problem Statement :page_with_curl:

Given two strings, `s` and `t`, find the number of distinct subsequences of `s` that are equal to `t`.

## Approach :bulb:

We'll use dynamic programming to solve this problem. Our goal is to build a 2D table to keep track of the number of distinct subsequences. We'll use modulo arithmetic to avoid overflow.

1. **Initialization** :seedling:
- We calculate the lengths of strings `s` and `t` and store them as `m` and `n`, respectively.
- If `m` is less than `n`, there can be no distinct subsequences, so we return 0.

2. **Dynamic Programming Matrix** :chart_with_upwards_trend:
- We create a 2D matrix `dp` with dimensions `m x n` to store the intermediate results.
- We initialize `dp[0][0]` based on whether the first characters of `s` and `t` match.

3. **Initialize First Column** :arrow_down:
- We iterate over the first column of `dp` (i.e., for `j = 0`) and calculate values based on whether `s[i]` and `t[0]` match:
- If they match, we add 1 to the value from the previous row and take the result modulo `1e9 + 7`.
- If they don't match, we simply copy the value from the previous row.

4. **Fill the Rest of the Matrix** :arrow_right:
- We iterate over the rest of the matrix using nested loops for `i` and `j`.
- For each cell, we check if `s[i]` and `t[j]` match:
- If they match, we add the values from the previous row and the diagonal and take the result modulo `1e9 + 7`.
- If they don't match, we copy the value from the previous row.

5. **Final Result** :tada:
- The final result is stored in `dp[m-1][n-1]`, which represents the number of distinct subsequences.

6. **Return Result** :rocket:
- We return `dp[m-1][n-1]`, which is the answer to the problem.

## Time Complexity :hourglass:
The time complexity of this solution is O(m * n), where `m` and `n` are the lengths of strings `s` and `t`, respectively.

## Space Complexity :file_folder:
The space complexity is O(m * n) because we use a 2D matrix of the same dimensions to store intermediate results.

## Happy Coding! :computer: :sparkles:
34 changes: 34 additions & 0 deletions Hard/115. Distinct Subsequences/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution {
public:
int numDistinct(string s, string t) {
int m = s.size(), n = t.size(), mod = 1e9+7;
if(m < n) return 0; // If the length of string s is less than the length of string t, there can be no distinct subsequences, so return 0.

// Create a 2D vector dp to store the number of distinct subsequences.
vector<vector<int>> dp(m, vector<int>(n, 0));

// Initialize dp[0][0] based on whether the first characters of s and t match.
dp[0][0] = s[0] == t[0];

// Initialize the first column of dp.
for(int i = 1; i < m; i++) {
if(s[i] == t[0])
dp[i][0] = (dp[i-1][0] % mod + 1) % mod; // If the characters match, add 1 to the previous value.
else
dp[i][0] = dp[i-1][0]; // If the characters don't match, copy the value from the previous row.
}

// Fill in the rest of the dp matrix.
for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
if(s[i] == t[j])
dp[i][j] = (dp[i-1][j] % mod + dp[i-1][j-1] % mod) % mod; // Characters match, so add values from the previous row and the diagonal.
else
dp[i][j] = dp[i-1][j]; // Characters don't match, so copy the value from the previous row.
}
}

// The final result is stored in dp[m-1][n-1], which represents the number of distinct subsequences.
return dp[m-1][n-1];
}
};
16 changes: 16 additions & 0 deletions Hard/115. Distinct Subsequences/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int numDistinct(String s, String t) {

int[] prev = new int[t.length()+1];
prev[0]=1;

for(int i =1; i<=s.length(); i++){
for(int j =t.length(); j>=1; j--){
if(s.charAt(i-1)==t.charAt(j-1)){
prev[j] = prev[j-1] + prev[j];
}
}
}
return prev[t.length()];
}
}
1 change: 1 addition & 0 deletions Hard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
| **41** | [First Missing Positive](41.%20First%20Missing%20Positive/) |
| **68** | [Test Justification](68.%20Test%20Justification/) |
| **72** | [Edit Distance](72.%20Edit%20Distance/) |
| **115** | [Distinct Subsequences](115.%20Distinct%20Subsequences/) |
| **135** | [Candy](135.%20Candy/) |
| **239** | [Sliding Window Max](239.%20Sliding%20Window%20Max/) |
| **332** | [Reconstruct Itinerary](332.%20Reconstruct%20Itinerary/) |
Expand Down

0 comments on commit c415a9d

Please sign in to comment.