public class Solution {
public int numDistinct(String S, String T) {
// Start typing your Java solution below
// DO NOT write main() function
if(S==null||T==null)return 0;
int lent = T.length();
int lens = S.length();
if(lens<lent)return 0;
int[][] dp = new int[lent][lens];
dp[0][0] = S.charAt(0)==T.charAt(0)?1:0;
for(int j = 1; j < lens; j++){
if(S.charAt(j)==T.charAt(0))dp[0][j] = dp[0][j-1]+1;
else dp[0][j] = dp[0][j-1];
}
for(int i = 1; i < lent; i++)
dp[i][0] = 0;
for(int i = 1; i < lent; i++)
for(int j = 1; j < lens; j++){
if(S.charAt(j)==T.charAt(i))dp[i][j] = dp[i-1][j-1] + dp[i][j-1];
else dp[i][j] = dp[i][j-1];
}
return dp[lent-1][lens-1];
}
}