-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDuplicatePath.java
More file actions
67 lines (36 loc) · 1.46 KB
/
Copy pathDuplicatePath.java
File metadata and controls
67 lines (36 loc) · 1.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
class Solution {
public List<List<String>> findDuplicate(String[] paths) {
List<List<String>> duplicates = new ArrayList<>();
Map<String, List<String>> indexPath = new HashMap<>();
if (paths.length == 0) {
return duplicates;
}
for (int i = 0; i < paths.length; i++) {
String path = paths[i];
String[] patharr = path.split("\\s+");
String root = patharr[0];
for (int j = 1; j < patharr.length; j++) {
int firstpos = patharr[j].indexOf("(");
int lastpos = patharr[j].indexOf(")");
String content = patharr[j].substring(firstpos + 1, lastpos);
String fullpath = root + "/" + patharr[j].substring(0, firstpos);
if (indexPath.containsKey(content)) {
List<String> temp = indexPath.get(content);
temp.add(fullpath);
indexPath.put(content, temp);
} else {
List<String> temp = new ArrayList<>();
temp.add(fullpath);
indexPath.put(content, temp);
}
}
}
for (Map.Entry<String, List<String>> entry : indexPath.entrySet()) {
List<String> value = entry.getValue();
if (value.size() > 1) {
duplicates.add(value);
}
}
return duplicates;
}
}