-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_115.java
More file actions
34 lines (30 loc) · 800 Bytes
/
Copy pathP_115.java
File metadata and controls
34 lines (30 loc) · 800 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
26
27
28
29
30
31
32
33
34
package leetcode.hard;
public class P_115 {
static boolean[][] seen;
static int[][] dp;
public int numDistinct(String s, String t) {
final int n = s.length();
final int m = t.length();
seen = new boolean[n][m];
dp = new int[n][m];
return dfs(s.toCharArray(), t.toCharArray(), 0, 0);
}
private static int dfs(char[] s, char[] t, int i, int j) {
if (j == t.length) {
return 1;
}
if (i == s.length) {
return 0;
}
if (seen[i][j]) {
return dp[i][j];
}
int res = 0;
if (s[i] == t[j]) {
res += dfs(s, t, i + 1, j + 1);
}
res += dfs(s, t, i + 1, j);
seen[i][j] = true;
return dp[i][j] = res;
}
}