-
Notifications
You must be signed in to change notification settings - Fork 496
/
LongestCommonSubsequence.java
41 lines (40 loc) · 1.34 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
40
41
public class LongestCommonSubsequence {
//Function that returns Longest Common Subsequence of two strings
//Time Complexity - O( len(str1) * len(str2) )
//Space Complexity - O( len(str1)*len(str2) )
private static String longestCommonSubsequence(String str1, String str2) {
int[][] arr = new int[str1.length() + 1][str2.length() + 1];
for (int i = str1.length() - 1; i >= 0; i--) {
for (int j = str2.length() - 1; j >= 0; j--) {
if (str1.charAt(i) == str2.charAt(j)) {
arr[i][j] = arr[i + 1][j + 1] + 1;
}
else {
arr[i][j] = Math.max(arr[i + 1][j], arr[i][j + 1]);
}
}
}
String res = "";
int i = 0;
int j = 0;
while (i < str1.length() && j < str2.length()) {
if (str1.charAt(i) == str2.charAt(j)) {
res = res + str1.charAt(i);
i++;
j++;
}
else if (arr[i + 1][j] >= arr[i][j + 1]) {
i++;
}
else {
j++;
}
}
return res;
}
public static void main(String[] args) {
String s1 = "applebanana";
String s2 = "alphabet";
System.out.println(longestCommonSubsequence(s1, s2));
}
}