-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
40 lines (37 loc) · 1.02 KB
/
Solution.java
File metadata and controls
40 lines (37 loc) · 1.02 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
package leetCode_115;
/**
* @author dimdark
* @date 2017-09-23
* @time 10:57 AM
*/
public class Solution {
public int numDistinct(String s, String t) {
if (s == null || t == null) return 0;// im-possible case
char[] cs = s.toCharArray();
char[] ct = t.toCharArray();
int[] dp = new int[cs.length + 1];
// initial
dp[0] = 1;
for (int i = 1; i <= cs.length ; ++i) {
dp[i] = 0;
}
int preSum = 0, tmpValue;
for (int i = 1; i <= ct.length; ++i) {
for (int j = 0; j <= cs.length; ++j) {
if (j != 0) {
tmpValue = dp[j];
dp[j] = (ct[i-1] == cs[j-1]) ? preSum : 0;
preSum += tmpValue;
} else {
dp[j] = 0;
preSum = (i != 1) ? 0 : 1;
}
}
}
int cnt = 0;
for (int i = 0; i <= cs.length; ++i) {
cnt += dp[i];
}
return cnt;
}
}