-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistinctSebsequenceTotal.java
More file actions
49 lines (39 loc) · 1.17 KB
/
DistinctSebsequenceTotal.java
File metadata and controls
49 lines (39 loc) · 1.17 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package codingInterview;
public class DistinctSebsequenceTotal {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "rabbbit";
String t = "rabit";
int total = distinctSubsequenceTotal(s, t);
System.out.println(total);
}
// dynamic programming : pay attention to the changing condition
private static int distinctSubsequenceTotal(String s, String t) {
// TODO Auto-generated method stub
int[][] table = new int[s.length() + 1][t.length() + 1];
for (int i = 0; i < s.length(); i++) {
table[i][0] = 1;
}
for (int i = 1; i < s.length() + 1; i++) {
for (int j = 1; j < t.length() + 1; j++) {
if (s.charAt(i - 1) == t.charAt(j - 1)) {
table[i][j] += table[i - 1][j - 1] + table[i - 1][j];
} else {
table[i][j] += table[i - 1][j];
}
}
}
printTable(table, s.length() + 1, t.length() + 1);
return table[s.length()][t.length()];
}
private static void printTable(int[][] table, int m, int n) {
// TODO Auto-generated method stub
for (int i = 0; i < m; i++) {
System.out.println();
for (int j = 0; j < n; j++) {
System.out.print(table[i][j]);
}
}
System.out.println();
}
}