From 6ab11cedd1dd82f97baa16ad480aac4678d30366 Mon Sep 17 00:00:00 2001 From: d982h8st7 Date: Tue, 31 Oct 2023 14:19:40 +0900 Subject: [PATCH 1/5] Create .md file of problem 115. --- Hard/115. Distinct Subsequences/Approach 1/sol.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Hard/115. Distinct Subsequences/Approach 1/sol.md diff --git a/Hard/115. Distinct Subsequences/Approach 1/sol.md b/Hard/115. Distinct Subsequences/Approach 1/sol.md new file mode 100644 index 0000000..d13d113 --- /dev/null +++ b/Hard/115. Distinct Subsequences/Approach 1/sol.md @@ -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. + From bf4b90b65ca7bcf6e4d5563febddb7ea7f1df8cb Mon Sep 17 00:00:00 2001 From: d982h8st7 Date: Tue, 31 Oct 2023 14:20:01 +0900 Subject: [PATCH 2/5] Create python solution of problem 115. --- .../Approach 1/solution.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Hard/115. Distinct Subsequences/Approach 1/solution.py diff --git a/Hard/115. Distinct Subsequences/Approach 1/solution.py b/Hard/115. Distinct Subsequences/Approach 1/solution.py new file mode 100644 index 0000000..8253c53 --- /dev/null +++ b/Hard/115. Distinct Subsequences/Approach 1/solution.py @@ -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] \ No newline at end of file From 2da5de511ccfec07852538c86944423c63da4be1 Mon Sep 17 00:00:00 2001 From: d982h8st7 Date: Tue, 31 Oct 2023 14:25:50 +0900 Subject: [PATCH 3/5] Create Rust solution of problem 115. --- .../Approach 1/solution.rust | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Hard/115. Distinct Subsequences/Approach 1/solution.rust diff --git a/Hard/115. Distinct Subsequences/Approach 1/solution.rust b/Hard/115. Distinct Subsequences/Approach 1/solution.rust new file mode 100644 index 0000000..19ed4fc --- /dev/null +++ b/Hard/115. Distinct Subsequences/Approach 1/solution.rust @@ -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] + } +} From 1b9858a32fd2a4f05819e9446e4c02f32b825476 Mon Sep 17 00:00:00 2001 From: d982h8st7 Date: Tue, 31 Oct 2023 14:26:02 +0900 Subject: [PATCH 4/5] Create Golang solution of problem 115. --- .../Approach 1/solution.go | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Hard/115. Distinct Subsequences/Approach 1/solution.go diff --git a/Hard/115. Distinct Subsequences/Approach 1/solution.go b/Hard/115. Distinct Subsequences/Approach 1/solution.go new file mode 100644 index 0000000..16fd06a --- /dev/null +++ b/Hard/115. Distinct Subsequences/Approach 1/solution.go @@ -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] +} From e4a6e27c9524872f42a743e6aec6314a441024b7 Mon Sep 17 00:00:00 2001 From: d982h8st7 Date: Tue, 31 Oct 2023 14:27:41 +0900 Subject: [PATCH 5/5] Create Scala solution of problem 115. --- .../Approach 1/solution.scala | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Hard/115. Distinct Subsequences/Approach 1/solution.scala diff --git a/Hard/115. Distinct Subsequences/Approach 1/solution.scala b/Hard/115. Distinct Subsequences/Approach 1/solution.scala new file mode 100644 index 0000000..1bfcc24 --- /dev/null +++ b/Hard/115. Distinct Subsequences/Approach 1/solution.scala @@ -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) + } +}