Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ac351c1

Browse files
authored
Merge pull request #3 from Megha1209/patch-1
Create lc940 java
2 parents 5efbb6f + dde6bb0 commit ac351c1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎code/lc940 java

Lines changed: 21 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /