Skip to content

Commit b893da6

Browse files
authored
Create 940-distinct-subsequences-ii.js
1 parent 45a5e75 commit b893da6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

940-distinct-subsequences-ii.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} S
3+
* @return {number}
4+
*/
5+
const distinctSubseqII = function(S) {
6+
// let end = new Array(26).fill(0), res = 0, added = 0, mod = 10 ** 9 + 7;
7+
// const aCode = ('a').charCodeAt(0)
8+
// for (let c of S) {
9+
// added = (res + 1 - end[c.charCodeAt(0) - aCode]) % mod;
10+
// res = (res + added) % mod;
11+
// end[c.charCodeAt(0) - aCode] = (end[c.charCodeAt(0) - aCode] + added) % mod;
12+
// }
13+
// return (res + mod) % mod;
14+
const m = new Map(),
15+
dp = [1],
16+
M = 1000000007
17+
for (let i = 0; i < S.length; i++) {
18+
const c = S.charAt(i)
19+
let prev = 0
20+
if (m.has(c)) {
21+
prev = dp[m.get(c)]
22+
}
23+
m.set(c, i)
24+
dp.push((((dp[i] * 2) % M) - prev) % M)
25+
if (dp[i + 1] < 0) {
26+
dp[i + 1] += M
27+
}
28+
}
29+
return dp[S.length] - 1
30+
}

0 commit comments

Comments
 (0)