-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.java
79 lines (68 loc) · 2.54 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
class Diffy{
private static String[][] dp;
// read the file line by line and store in list as string
private static List<String> readContent(String path) throws Exception{
String content = new String(Files.readAllBytes(Paths.get(path)));
Scanner sc = new Scanner(content);
List<String> contentArray = new ArrayList<>();
while(sc.hasNextLine()){
String line = sc.nextLine();
contentArray.add(line);
}
sc.close();
return contentArray;
}
// move both i and j ptrs if characters match else
// move 1 pointer at a time
private static String findLCSHelper(String a,String b,int i,int j){
if (i < 0 || j < 0) {
return "";
}
if (dp[i][j] != null) {
return dp[i][j];
}
if (a.charAt(i) == b.charAt(j)) {
String result = findLCSHelper(a, b, i - 1, j - 1) + a.charAt(i);
return dp[i][j] = result;
}
String lcs1 = findLCSHelper(a, b, i - 1, j);
String lcs2 = findLCSHelper(a, b, i, j - 1);
String result = (lcs1.length() > lcs2.length()) ? lcs1 : lcs2;
return dp[i][j] = result;
}
// memoization with dp array
private static void LCSCompare(String content1,String content2,String[] maxString){
int m = content1.length();
int n = content2.length();
dp = new String[m][n];
for (String[] v:dp) {
Arrays.fill(v,null);
}
String result = findLCSHelper(content1,content2,content1.length()-1,content2.length()-1);
if(maxString[0].length() < result.length()){
maxString[0] = result;
}
}
// read the files and compare them line by line
public static void main(String[] args) throws Exception{
List<String> content1Array = readContent(args[0]);
List<String> content2Array = readContent(args[1]);
String ans = "";
for(int i=0;i<content1Array.size() && i<content2Array.size();i++){
String[] maxString = new String[1];
maxString[0] = "";
LCSCompare(content1Array.get(i),content2Array.get(i),maxString);
if(maxString[0].length() == content1Array.get(i).length()){
continue;
}
else {
ans+="1. "+content1Array.get(i)+"\n";
ans+="2. "+content2Array.get(i)+"\n";
}
}
System.out.println(ans);
}
}