forked from anishLearnsToCode/leetcode-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestCommonSubsequence.java
39 lines (35 loc) · 1.39 KB
/
LongestCommonSubsequence.java
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
public class LongestCommonSubsequence {
public int longestCommonSubsequence(String text1, String text2) {
final int[][] memory = new int[2][text2.length()];
final int rows = text1.length(), columns = text2.length();
// first row, first column
memory[0][0] = text1.charAt(0) == text2.charAt(0) ? 1 : 0;
// first row
for (int column = 1 ; column < columns ; column++) {
memory[0][column] = text2.charAt(column) == text1.charAt(0) ? 1 : memory[0][column - 1];
}
int i = 1;
// rest of table
for (int row = 1 ; row < rows ; row++, i ^= 1) {
for (int column = 0 ; column < text2.length() ; column++) {
if (column == 0) {
memory[i][column] = text1.charAt(row) == text2.charAt(0) ? 1 : memory[i ^ 1][column];
} else {
memory[i][column] = max(
memory[i][column - 1],
memory[i ^ 1][column],
text1.charAt(row) == text2.charAt(column) ? 1 + memory[i ^ 1][column - 1] : 0
);
}
}
}
return memory[i ^ 1][columns - 1];
}
private int max(int... values) {
int result = 0;
for (int value : values) {
result = Math.max(result, value);
}
return result;
}
}