Skip to content

Commit 239bf31

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

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

115-distinct-subsequences.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,27 @@ const numDistinct = function(s, t) {
2323
}
2424
return mem[tlen][slen]
2525
}
26+
27+
28+
// another
29+
30+
/**
31+
* @param {string} s
32+
* @param {string} t
33+
* @return {number}
34+
*/
35+
const numDistinct = function(s, t) {
36+
const m = t.length,
37+
n = s.length
38+
const cur = new Array(m + 1).fill(0)
39+
cur[0] = 1
40+
for (let j = 1; j <= n; j++) {
41+
let pre = 1
42+
for (let i = 1; i <= m; i++) {
43+
let temp = cur[i]
44+
cur[i] = cur[i] + (t[i - 1] == s[j - 1] ? pre : 0)
45+
pre = temp
46+
}
47+
}
48+
return cur[m]
49+
}

0 commit comments

Comments
 (0)