```
int numDistinct(string S, string T) {
int m = S.length();
int n = T.length();
vector<int> sub(n+1, 0);
sub[0] = 1;
for (int i = 0; i < m; i++) {
for (int j = n; j > 0; j--) {
if (S[i] == T[j-1]) {
sub[j] += sub[j-1];
}
}
}
return sub[n];
//T[i][j] = S[i] == T[j] T[i-1][j-1] + T[i-1][j]
// S[i] != T[j] T[i-1][j]
}
```