-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlongest_common_subsequence.java
More file actions
31 lines (27 loc) · 1009 Bytes
/
longest_common_subsequence.java
File metadata and controls
31 lines (27 loc) · 1009 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
public static String lcs(String a, String b) {
int[][] lengths = new int[a.length() + 1][b.length() + 1];
// row 0 and column 0 are initialized to 0 already
for (int i = 0; i < a.length(); i += 1)
for (int j = 0; j < b.length(); j += 1)
if (a.charAt(i) == b.charAt(j))
lengths[i + 1][j + 1] = lengths[i][j] + 1;
else
lengths[i + 1][j + 1] =
Math.max(lengths[i + 1][j], lengths[i][j + 1]);
// read the substring out from the matrix
StringBuffer sb = new StringBuffer();
for (int x = a.length(), y = b.length();
x != 0 && y != 0; ) {
if (lengths[x][y] == lengths[x-1][y])
x--;
else if (lengths[x][y] == lengths[x][y-1])
y--;
else {
assert a.charAt(x-1) == b.charAt(y-1);
sb.append(a.charAt(x-1));
x--;
y--;
}
}
return sb.reverse().toString();
}