-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathFindDuplicateFileinSystem.java
More file actions
36 lines (27 loc) · 1.04 KB
/
FindDuplicateFileinSystem.java
File metadata and controls
36 lines (27 loc) · 1.04 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
class Solution {
public List<List<String>> findDuplicate(String[] paths) {
// <Content, List<FilePath>>
Map<String, List<String>> map = new HashMap<>();
//"root/a 1.txt(abcd) 2.txt(efgh)
for(String path : paths){
String[] dir = path.split(" ");
String rootPath = dir[0]; // root/a
for(int i=1;i<dir.length;i++){
//1.txt(abcd)
String content = dir[i].substring(dir[i].indexOf("("),dir[i].indexOf(")"));
if(!map.containsKey(content)) {
map.put(content,new ArrayList<>());
}
//1.txt(abcd)
map.get(content).add(rootPath +"/" +dir[i].substring(0 ,dir[i].indexOf("(")));
}
}
List<List<String>> list = new ArrayList<>();
for(Map.Entry<String,List<String>> entry : map.entrySet()){
// check for duplication
if(entry.getValue().size()>1)
list.add(entry.getValue());
}
return list;
}
}