forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
25 lines (21 loc) · 792 Bytes
/
Solution.java
File metadata and controls
25 lines (21 loc) · 792 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class Solution {
public int numDistinct(String S, String T) {
// how many ways in s[0..i] can make t[0..j]
int[][] dp = new int[S.length() + 1][T.length() + 1];
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++) {
if (S.charAt(i - 1) == T.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1];
} else {
dp[i][j] = dp[i - 1][j];
}
}
}
return dp[S.length()][T.length()];
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.numDistinct("rabbbit", "rabbit"));
}
}