Skip to content

Commit 15b5dc4

Browse files
authored
solution(go,python,rust,scala): 115. Distinct Subsequences
115. Distinct Subsequences - Go - Python - Rust - Scala
2 parents c415a9d + e4a6e27 commit 15b5dc4

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
1. The function numDistinct is designed to determine the number of distinct subsequences of string s which equals string t.
2+
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+
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+
4. For each character in s and t, the function updates the dp array based on the following conditions:
5+
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+
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+
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.
8+
9+
10+
## Time Complexity:
11+
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.
12+
13+
## Space Complexity:
14+
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.
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func numDistinct(s string, t string) int {
2+
m := len(s)
3+
n := len(t)
4+
5+
if m < n {
6+
return 0
7+
}
8+
9+
dp := make([][]int, m+1)
10+
for i := range dp {
11+
dp[i] = make([]int, n+1)
12+
dp[i][0] = 1
13+
}
14+
15+
for i := 1; i <= m; i++ {
16+
for j := 1; j <= n; j++ {
17+
if s[i-1] == t[j-1] {
18+
dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
19+
} else {
20+
dp[i][j] = dp[i-1][j]
21+
}
22+
}
23+
}
24+
25+
return dp[m][n]
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def numDistinct(self, s: str, t: str) -> int:
3+
m, n = len(s), len(t)
4+
dp = [[0] * (n + 1) for _ in range(m + 1)]
5+
6+
for i in range(m + 1):
7+
dp[i][0] = 1
8+
9+
for i in range(1, m + 1):
10+
for j in range(1, n + 1):
11+
if s[i - 1] == t[j - 1]:
12+
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j]
13+
else:
14+
dp[i][j] = dp[i - 1][j]
15+
16+
return dp[m][n]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn num_distinct(s: String, t: String) -> i32 {
3+
let m = s.len();
4+
let n = t.len();
5+
let s_bytes = s.as_bytes();
6+
let t_bytes = t.as_bytes();
7+
8+
if m < n {
9+
return 0;
10+
}
11+
12+
let mut dp = vec![vec![0; n + 1]; m + 1];
13+
14+
for i in 0..=m {
15+
dp[i][0] = 1;
16+
}
17+
18+
for i in 1..=m {
19+
for j in 1..=n {
20+
if s_bytes[i - 1] == t_bytes[j - 1] {
21+
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
22+
} else {
23+
dp[i][j] = dp[i - 1][j];
24+
}
25+
}
26+
}
27+
28+
dp[m][n]
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
object Solution {
2+
def numDistinct(s: String, t: String): Int = {
3+
val m = s.length
4+
val n = t.length
5+
6+
if (m < n) {
7+
return 0
8+
}
9+
10+
val dp: Array[Array[Int]] = Array.fill(m + 1, n + 1)(0)
11+
for (i <- 0 to m) {
12+
dp(i)(0) = 1
13+
}
14+
15+
for (i <- 1 to m) {
16+
for (j <- 1 to n) {
17+
if (s(i-1) == t(j-1)) {
18+
dp(i)(j) = dp(i-1)(j-1) + dp(i-1)(j)
19+
} else {
20+
dp(i)(j) = dp(i-1)(j)
21+
}
22+
}
23+
}
24+
25+
dp(m)(n)
26+
}
27+
}

0 commit comments

Comments
 (0)