-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_1143.java
More file actions
48 lines (43 loc) · 1.42 KB
/
Copy pathP_1143.java
File metadata and controls
48 lines (43 loc) · 1.42 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
package leetcode.medium;
public class P_1143 {
public int longestCommonSubsequenceBottomUp(String text1, String text2) {
final int n = text1.length();
final int m = text2.length();
final int[][] dp = new int[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
}
}
}
return dp[n][m];
}
static boolean[][] seen;
static int[][] dp;
public int longestCommonSubsequence(String text1, String text2) {
final int n = text1.length();
final int m = text2.length();
seen = new boolean[n][m];
dp = new int[n][m];
return dfs(text1.toCharArray(), text2.toCharArray(), 0, 0);
}
private static int dfs(char[] l, char[] r, int i, int j) {
if (i == l.length || j == r.length) {
return 0;
}
if (seen[i][j]) {
return dp[i][j];
}
int res = 0;
res = Math.max(res, dfs(l, r, i + 1, j));
res = Math.max(res, dfs(l, r, i, j + 1));
if (l[i] == r[j]) {
res = Math.max(res, 1 + dfs(l, r, i + 1, j + 1));
}
seen[i][j] = true;
return dp[i][j] = res;
}
}