Actually, I start from S.substring(0, 1) and T.substring(0, 1). Then continue add one element to S and T.

When I try to add S.charAt(i + 1) to S.substring(0, i + 1), and T.charAt(j + 1) to T.substring(0, j + 1).

If S.charAt(i + 1) == T.charAt(j + 1) then the numDistinct(S.substring(0, i + 2), T.substring(0, j + 2)) should be numDistinct(S.substring(0, i + 1), T.substring(0, j + 1)) (use the new element in the sub string) + numDistinct(S.substring(0, i + 1), T.substring(0, j + 2)) (don't use the new element in the sub string).

but if S.charAt(i + 1) != T.charAt(j + 1) then we can't use the new element in the sub string so numDistinct(S.substring(0, i + 2), T.substring(0, j + 2)) = numDistinct(S.substring(0, i + 1), T.substring(0, j + 2)).

```
public int numDistinct(final String S, final String T)
{
final int[][] results = new int[T.length()+1][S.length()+1];
for (int i = 0; i < S.length(); ++i)
{
results[0][i] = 1;
}
for (int i = 0; i < S.length(); ++i)
{
for (int j = 0; j < T.length(); ++j)
{
if (S.charAt(i) == T.charAt(j))
{
results[j + 1][i + 1] = results[j][i] + results[j + 1][i];
}
else
{
results[j + 1][i + 1] = results[j + 1][i];
}
}
}
return results[T.length()][S.length()];
}
```