未验证 提交 dde6bb02 编写于 作者: M Megha1209 提交者: GitHub

Create lc940 java

Leetcode Question no 940 DP solution.
上级 5efbb6fa
public static int distinctSubseqII(String S) {
int[] dp=new int[S.length()+1];
int[] lastSeen=new int[26];
dp[0]=1;
int mod=(int)1e9+7;
for(int i=1;i<dp.length();i++){
dp[i] = (dp[i-1] * 2) % mod;
int idx=S.charAt(i-1)-'a';
if(lastSeen[idx]!=0)
dp[i] -= dp[lastSeen[idx]-1];
dp[i]%=mod;
lastSeen[idx]=i;
}
int ans = --dp[S.length()];
if(ans<0) ans+=mod;
return ans;
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册