Skip to content

Commit

Permalink
solution(go,python,rust,scala): 115. Distinct Subsequences
Browse files Browse the repository at this point in the history
115. Distinct Subsequences
- Go
- Python
- Rust
- Scala
  • Loading branch information
godkingjay authored Oct 31, 2023
2 parents c415a9d + e4a6e27 commit 15b5dc4
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Hard/115. Distinct Subsequences/Approach 1/sol.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1. The function numDistinct is designed to determine the number of distinct subsequences of string s which equals string t.
2. To achieve this, the function leverages a dynamic programming approach using a 2D array dp. The element dp[i][j] represents the number of distinct subsequences of the first i characters of s that equal the first j characters of t.
3. The function initializes the first column of dp to 1 because there's always one way to form an empty subsequence from any string.
4. For each character in s and t, the function updates the dp array based on the following conditions:
5. If the current characters of s and t match, the number of subsequences would be the sum of the subsequences without the current character in both s and t (i.e., dp[i-1][j-1]) and the subsequences without the current character in s (i.e., dp[i-1][j]).
6. If the characters don't match, the number of subsequences would be the same as the number of subsequences without the current character in s.
7. After processing all characters, the function returns the value dp[m][n], which represents the total number of distinct subsequences of s that equal t.


## Time Complexity:
The time complexity of the numDistinct function is O(m * n), where m is the length of string s and n is the length of string t. This is because the function iterates through each character of s and t once to update the dp array.

## Space Complexity:
The space complexity is also O(m * n) due to the 2D array dp used to store the number of distinct subsequences at each position. The size of this array scales with the lengths of s and t.

26 changes: 26 additions & 0 deletions Hard/115. Distinct Subsequences/Approach 1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
func numDistinct(s string, t string) int {
m := len(s)
n := len(t)

if m < n {
return 0
}

dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
dp[i][0] = 1
}

for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
if s[i-1] == t[j-1] {
dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
} else {
dp[i][j] = dp[i-1][j]
}
}
}

return dp[m][n]
}
16 changes: 16 additions & 0 deletions Hard/115. Distinct Subsequences/Approach 1/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution:
def numDistinct(self, s: str, t: str) -> int:
m, n = len(s), len(t)
dp = [[0] * (n + 1) for _ in range(m + 1)]

for i in range(m + 1):
dp[i][0] = 1

for i in range(1, m + 1):
for j in range(1, n + 1):
if s[i - 1] == t[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
else:
dp[i][j] = dp[i - 1][j]

return dp[m][n]
30 changes: 30 additions & 0 deletions Hard/115. Distinct Subsequences/Approach 1/solution.rust
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
impl Solution {
pub fn num_distinct(s: String, t: String) -> i32 {
let m = s.len();
let n = t.len();
let s_bytes = s.as_bytes();
let t_bytes = t.as_bytes();

if m < n {
return 0;
}

let mut dp = vec![vec![0; n + 1]; m + 1];

for i in 0..=m {
dp[i][0] = 1;
}

for i in 1..=m {
for j in 1..=n {
if s_bytes[i - 1] == t_bytes[j - 1] {
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
} else {
dp[i][j] = dp[i - 1][j];
}
}
}

dp[m][n]
}
}
27 changes: 27 additions & 0 deletions Hard/115. Distinct Subsequences/Approach 1/solution.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
object Solution {
def numDistinct(s: String, t: String): Int = {
val m = s.length
val n = t.length

if (m < n) {
return 0
}

val dp: Array[Array[Int]] = Array.fill(m + 1, n + 1)(0)
for (i <- 0 to m) {
dp(i)(0) = 1
}

for (i <- 1 to m) {
for (j <- 1 to n) {
if (s(i-1) == t(j-1)) {
dp(i)(j) = dp(i-1)(j-1) + dp(i-1)(j)
} else {
dp(i)(j) = dp(i-1)(j)
}
}
}

dp(m)(n)
}
}

0 comments on commit 15b5dc4

Please sign in to comment.