Java版本代码1
import java.util.*;
public class Solution {
public int numDistinct (String S, String T) {
// write code here
char[] s = S.toCharArray();
char[] t = T.toCharArray();
int[][] dp = new int[s.length + 1][t.length + 1];
for (int j = 0; j <= t.length; j++) {
dp[0][j] = 0;
}
for (int i = 0; i <= s.length; i++) {
dp[i][0] = 1;
}
for (int i = 1; i <= s.length; i++) {
for (int j = 1; j <= t.length; j++) {
dp[i][j] = dp[i - 1][j] + (s[i - 1] == t[j - 1] ? dp[i - 1][j - 1] : 0);
}
}
return dp[s.length][t.length];
}
}
Java版本代码2 import java.util.*;
public class Solution {
public int numDistinct (String S, String T) {
// write code here
char[] s = S.toCharArray();
char[] t = T.toCharArray();
int[] dp = new int[t.length + 1];
dp[0] = 1;
for (int j = 1; j <= t.length; j++) {
dp[j] = 0;
}
for (int i = 1; i <= s.length; i++) {
for (int j = t.length; j >= 1; j--) {
dp[j] += s[i - 1] == t[j - 1] ? dp[j - 1] : 0;
}
}
return dp[t.length];// write code here
}
}