DP leetcode 903. Valid Permutations for DI Sequence

class Solution:
def numPermsDISequence(self, S: str) -> int:
dp = [1]*(len(S)+1)
for s in S:
if s=='D':
dp = dp[:-1]
for i in range(1, len(dp)):
dp[i] += dp[i-1]
else:
dp = dp[1:]
for i in range(len(dp)-1)[::-1]:
dp[i]+=dp[i+1]
return dp[0] % (10**9+7)
class Solution:
def numPermsDISequence(self, S: str) -> int:
dp = [1]*(len(S)+1)
for s in S:
if s=='I':
dp = dp[:-1]
for i in range(1, len(dp)):
dp[i] += dp[i-1]
else:
dp = dp[1:]
for i in range(len(dp)-1)[::-1]:
dp[i]+=dp[i+1]
return dp[0] % (10**9+7)

Data Scientist/MLE/SWE @takemobi