-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solution(cpp,java): 115. Distinct Subsequences
115. Distinct Subsequences - C++ - Java
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
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 @@ | ||
# 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: |
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,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]; | ||
} | ||
}; |
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,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()]; | ||
} | ||
} |
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