dp
with dimensions (n+1) x (n+1)
and set all elements to 0.dp[0][j]
to 1 for all j
because there is only one way to have a permutation of length 1.s
from left to right.s[i]
, update dp[i+1][j]
based on the following rules:
s[i] == 'I'
, set dp[i+1][j]
to the sum of dp[i][0]
to dp[i][j-1]
.s[i] == 'D'
, set dp[i+1][j]
to the sum of dp[i][j]
to dp[i][n-i]
.dp
, i.e., sum(dp[n][0]
to dp[n][n])
.