- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdistinctSubsequences.java
More file actions
Latest commit
29 lines (27 loc) · 793 Bytes
/
Copy pathdistinctSubsequences.java
File metadata and controls
29 lines (27 loc) · 793 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//dynamic program
publicclassSolution {
publicintnumDistinct(StringS, StringT) {
if(S==null||T==null) {
return0;
}
if(S.length()<T.length()) {
return0;
}
int [][] dp = newint[S.length()+1][T.length()+1];
dp[0][0] = 1;
//It is only one way to change a i length string to "".
for(inti=0;i<S.length();i++) {
dp[i][0] = 1;
}
//dp
for(inti=1;i<=S.length();i++) {
for(intj=1;j<=T.length();j++) {
dp[i][j] = dp[i-1][j];
if(S.charAt(i-1)==T.charAt(j-1)) {
dp[i][j] += dp[i-1][j-1];
}
}
}
returndp[S.length()][T.length()];
}
}