-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongestCommonSubsequence.java
More file actions
59 lines (47 loc) · 1.52 KB
/
LongestCommonSubsequence.java
File metadata and controls
59 lines (47 loc) · 1.52 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.mycompany.algorithm_final_project;
import java.util.Scanner;
/**
*
* @author israkkayumchowdhury
*/
public class LongestCommonSubsequence {
public String lcs(String str1, String str2) {
int m = str1.length();
int n = str2.length();
int[][] dp = new int[m + 1][n + 1];
// Compute the length of lcs for all substrings
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
// Construct the lcs string
StringBuilder lcs = new StringBuilder();
int i = m, j = n;
while (i > 0 && j > 0) {
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
lcs.insert(0, str1.charAt(i - 1));
i--;
j--;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}
return lcs.toString();
}
public void main_func() {
Scanner s = new Scanner(System.in);
System.out.print(" Enter the first string: ");
String str1 = s.nextLine();
System.out.print(" Enter the second string: ");
String str2 = s.nextLine();
String lcs = lcs(str1, str2);
System.out.println(" Longest Common Subsequence: " + lcs);
}
}