Skip to content

Commit 2214b3f

Browse files
authored
Update 115-distinct-subsequences.js
1 parent 239bf31 commit 2214b3f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

115-distinct-subsequences.js

+21
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,24 @@ const numDistinct = function(s, t) {
4747
}
4848
return cur[m]
4949
}
50+
51+
// another
52+
53+
/**
54+
* @param {string} s
55+
* @param {string} t
56+
* @return {number}
57+
*/
58+
const numDistinct = function(s, t) {
59+
const m = t.length,
60+
n = s.length
61+
const dp = new Array(m + 1).fill(0)
62+
dp[0] = 1
63+
for (let j = 1; j <= n; j++) {
64+
for (let i = m; i > 0; i--) {
65+
dp[i] = dp[i] + (t[i - 1] == s[j - 1] ? dp[i - 1] : 0)
66+
}
67+
}
68+
return dp[m]
69+
}
70+

0 commit comments

Comments
 (0)