-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
29 lines (22 loc) · 1004 Bytes
/
LongestCommonSubsequence.java
File metadata and controls
29 lines (22 loc) · 1004 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
class Solution {
//Time: O(n*m) where n is text1 size and m is text2 size
//Space: O(n*m) where n is text1 size and m is text2 size
public int longestCommonSubsequence(String text1, String text2) {
int[][] longestSoFar = new int[text1.length() + 1][text2.length() + 1];
for (int i = 1; i < text1.length() + 1; i++) {
for (int j = 1; j < text2.length() + 1; j++) {
char char1 = text1.charAt(i - 1);
char char2 = text2.charAt(j - 1);
if (char1 == char2) {
longestSoFar[i][j] = 1 + longestSoFar[i-1][j-1];
} else {
longestSoFar[i][j] = Math.max(
longestSoFar[i-1][j],
longestSoFar[i][j-1]
);
}
}
}
return longestSoFar[text1.length()][text2.length()];
}
}