Skip to content

Commit a2d1651

Browse files
authored
Update 903-valid-permutations-for-di-sequence.js
1 parent e21aad2 commit a2d1651

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

903-valid-permutations-for-di-sequence.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
const numPermsDISequence = function(s) {
6+
const mod = 1e9 + 7
7+
const n = s.length
8+
const dp = Array.from({ length: n + 1 }, () => Array(n + 1).fill(0))
9+
s = '#' + s
10+
dp[0][0] = 1
11+
for(let i = 1; i <= n; i++) {
12+
const ch = s[i]
13+
for(let j = 0; j <= i; j++) {
14+
if(ch === 'I') {
15+
for(let k = 0; k < j; k++) {
16+
dp[i][j] += dp[i - 1][k]
17+
dp[i][j] %= mod
18+
}
19+
} else {
20+
for(let k = j; k < i; k++) {
21+
dp[i][j] += dp[i - 1][k]
22+
dp[i][j] %= mod
23+
}
24+
}
25+
}
26+
}
27+
28+
29+
let res = 0
30+
31+
for(let i = 0; i <= n; i++) {
32+
res = (res + dp[n][i]) % mod
33+
}
34+
35+
return res
36+
};
37+
38+
39+
// another
40+
41+
142
/**
243
* @param {string} s
344
* @return {number}

0 commit comments

Comments
 (0)