/**
* Solution (DP):
* We keep a m*n matrix and scanning through string S, while
* m = T.length() + 1 and n = S.length() + 1
* and each cell in matrix Path[i][j] means the number of distinct subsequences of
* T.substr(1...i) in S(1...j)
*
* Path[i][j] = Path[i][j-1] (discard S[j])
* + Path[i-1][j-1] (S[j] == T[i] and we are going to use S[j])
* or 0 (S[j] != T[i] so we could not use S[j])
* while Path[0][j] = 1 and Path[i][0] = 0.
*/
int numDistinct(string S, string T) {
int m = T.length();
int n = S.length();
if (m > n) return 0; // impossible for subsequence
vector<vector<int>> path(m+1, vector<int>(n+1, 0));
for (int k = 0; k <= n; k++) path[0][k] = 1; // initialization
for (int j = 1; j <= n; j++) {
for (int i = 1; i <= m; i++) {
path[i][j] = path[i][j-1] + (T[i-1] == S[j-1] ? path[i-1][j-1] : 0);
}
}
return path[m][n];
}
/**
* Further optimization could be made that we can use only 1D array instead of a
* matrix, since we only need data from last time step.
*/
int numDistinct(string S, string T) {
int m = T.length();
int n = S.length();
if (m > n) return 0; // impossible for subsequence
vector<int> path(m+1, 0);
path[0] = 1; // initial condition
for (int j = 1; j <= n; j++) {
// traversing backwards so we are using path[i-1] from last time step
for (int i = m; i >= 1; i--) {
path[i] = path[i] + (T[i-1] == S[j-1] ? path[i-1] : 0);
}
}
return path[m];
}
No comments:
Post a Comment