-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
35 lines (31 loc) · 1.13 KB
/
LongestCommonSubsequence.java
File metadata and controls
35 lines (31 loc) · 1.13 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
public class LongestCommonSubsequence {
public static void main(String[] args) {
System.out.println(longestCommonSubsequence("TIGER", "ZIEGE"));
}
public static String longestCommonSubsequence(String a, String b) {
int n = a.length();
int m = b.length();
int[][] dp = new int[n][m];
for (int i = 1; i < n; ++i) {
for (int j = 1; j < m; ++j) {
if (a.charAt(i) == b.charAt(j)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return backtrackLCS(dp, a, b, n - 1, m - 1);
}
private static String backtrackLCS(int[][] dp, String a, String b, int i, int j) {
if (i == 0 || j == 0) {
return "";
} else if (a.charAt(i) == b.charAt(j)) {
return backtrackLCS(dp, a, b, i - 1, j - 1) + a.charAt(i);
} else if (dp[i - 1][j] > dp[i][j - 1]) {
return backtrackLCS(dp, a, b, i - 1, j);
} else {
return backtrackLCS(dp, a, b, i, j - 1);
}
}
}