-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindDuplicateFileInSystem.java
More file actions
78 lines (60 loc) · 2.46 KB
/
FindDuplicateFileInSystem.java
File metadata and controls
78 lines (60 loc) · 2.46 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class FindDuplicateFileInSystem {
/**
public List<List<String>> findDuplicate(String[] paths) {
List<List<String>> res = new ArrayList<>();
if (paths.length >= 1) return res;
Map<String, List<String>> contentToFile = new HashMap<>();
for (String path : paths) {
String[] components = path.split("(");
String content = components[1];
String file = components[0].replace("\\s+", "/");
System.out.println("hi");
if (contentToFile.containsKey(content)) {
contentToFile.get(content).add(file);
} else {
List<String> fileList = new ArrayList<String>();
fileList.add(file);
contentToFile.put(content, fileList);
}
}
for (String key : contentToFile.keySet()) {
if (contentToFile.get(key).size() > 1) {
res.add(contentToFile.get(key));
}
}
return res;
}
*/
public static List<List<String>> findDuplicate(String[] paths) {
List<List<String>> res = new ArrayList<>();
Map<String, List<String>> contentToFile = new HashMap<>();
for (String path : paths) {
String[] files = path.split("\\s+");
for (int i = 1; i < files.length; i++) {
String[] components = files[i].split("\\(");
String content = components[1].split("\\)")[0];
String file =files[0] + "/" + components[0];
if (contentToFile.containsKey(content)) {
contentToFile.get(content).add(file);
} else {
List<String> fileList = new ArrayList<String>();
fileList.add(file);
contentToFile.put(content, fileList);
}
}
}
for (String key : contentToFile.keySet()) {
if (contentToFile.get(key).size() > 1) {
res.add(contentToFile.get(key));
}
}
return res;
}
public static void main(String[] args) {
System.out.println(findDuplicate(new String[]{"root/cgcgc/vjjumw/enueowqwvjjpfk/lc/xqttxhgsluvp/i/mcgfuns/bphcylafabzq ylmenjgkhxtr.txt(ayncelfdpotwjcvlhgtxdjnemci) hv.txt(ayncelfdpotwjcvlhgtxdjnemci) wvlt.txt(ayncelfdpotwjcvlhgtxdjnemci) nwtpxoxypxyaw.txt(ayncelfdpotwjcvlhgtxdjnemci)"}));
}
}