Distinct subsequences
* 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.//初始化class Solution {
public int numDistinct(String s, String t) {
int n = s.length();
int m = t.length();
if(m > n) return 0;
int[][] f = new int[n + 1][m + 1];
//不能在循环里初始化,是为了防止a,b的长度不足1时候。
for(int i=0; i< n+1; i++){
f[i][0] = 1; #对角线上的 需要给1开始。也可以是S全部删掉,至少有1种。
}#不要初始f[0][j]因为后面无论如何f[i][j] = f[i-1][j],该是0
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= m; j ++){
f[i][j] = f[i - 1][j] + (s.charAt(i-1) == t.charAt(j-1) ? f[i - 1][j - 1] : 0);
}
}
return f[n][m];
}
}Last updated