Skip to content

Commit dde6bb0

Browse files
authored
Create lc940 java
Leetcode Question no 940 DP solution.
1 parent 5efbb6f commit dde6bb0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

code/lc940 java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public static int distinctSubseqII(String S) {
2+
int[] dp=new int[S.length()+1];
3+
int[] lastSeen=new int[26];
4+
dp[0]=1;
5+
int mod=(int)1e9+7;
6+
7+
for(int i=1;i<dp.length();i++){
8+
dp[i] = (dp[i-1] * 2) % mod;
9+
10+
int idx=S.charAt(i-1)-'a';
11+
if(lastSeen[idx]!=0)
12+
dp[i] -= dp[lastSeen[idx]-1];
13+
14+
dp[i]%=mod;
15+
lastSeen[idx]=i;
16+
}
17+
18+
int ans = --dp[S.length()];
19+
if(ans<0) ans+=mod;
20+
return ans;
21+
}

0 commit comments

Comments
 (0)