Skip to content

Commit 1f3ede9

Browse files
authored
Create 115-distinct-subsequences.js
1 parent c77eba3 commit 1f3ede9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

115-distinct-subsequences.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {number}
5+
*/
6+
const numDistinct = function(s, t) {
7+
const tlen = t.length
8+
const slen = s.length
9+
const mem = Array.from({ length: tlen + 1 }, () =>
10+
new Array(slen + 1).fill(0)
11+
)
12+
for (let j = 0; j <= slen; j++) {
13+
mem[0][j] = 1
14+
}
15+
for (let i = 0; i < tlen; i++) {
16+
for (let j = 0; j < slen; j++) {
17+
if (t.charAt(i) === s.charAt(j)) {
18+
mem[i + 1][j + 1] = mem[i][j] + mem[i + 1][j]
19+
} else {
20+
mem[i + 1][j + 1] = mem[i + 1][j]
21+
}
22+
}
23+
}
24+
return mem[tlen][slen]
25+
}

0 commit comments

Comments
 (0)