|
1 | 1 | class Solution {
|
2 |
| - |
3 |
| - public List<List<String>> findDuplicate(String[] paths) { |
4 |
| - |
5 |
| - Map<String, ArrayList<String>> map = new HashMap<>(); |
6 |
| - List<List<String>> ans = new ArrayList<>(); |
7 |
| - |
8 |
| - for (String s : paths) { |
9 |
| - String[] temp = s.split("\\s"); |
10 |
| - |
11 |
| - for (int i=1;i<temp.length;i++) { |
12 |
| - String message = temp[i].substring(temp[i].indexOf("(")+1, temp[i].indexOf(")")); |
13 |
| - String fileName = temp[i].substring(0, temp[i].indexOf("(")); |
14 |
| - |
15 |
| - if (map.containsKey(message)) { |
16 |
| - ArrayList<String> tempList = map.get(message); |
17 |
| - tempList.add(temp[0] + "/" + fileName); |
18 |
| - map.put(message, tempList); |
19 |
| - } |
20 |
| - else { |
21 |
| - ArrayList<String> tempList = new ArrayList<>(); |
22 |
| - tempList.add(temp[0] + "/" + fileName); |
23 |
| - map.put(message, tempList); |
24 |
| - } |
25 |
| - |
26 |
| - } |
27 |
| - } |
28 |
| - |
29 |
| - for (Map.Entry<String,ArrayList<String>> entry : map.entrySet()) { |
30 |
| - |
31 |
| - List<String> l = entry.getValue(); |
32 |
| - |
33 |
| - if (l.size() > 1) { |
34 |
| - ans.add(l); |
35 |
| - } |
36 |
| - |
37 |
| - } |
38 |
| - |
39 |
| - return ans; |
| 2 | + public List<List<String>> findDuplicate(String[] paths) { |
| 3 | + Map<String, List<String>> map = new HashMap<>(); |
| 4 | + for (String path : paths) { |
| 5 | + String[] strs = path.split("\\s+"); |
| 6 | + String filePath = strs[0]; |
| 7 | + for (int i = 1; i < strs.length; i++) { |
| 8 | + int startIdx = strs[i].indexOf('('); |
| 9 | + String fileName = strs[i].substring(0, startIdx); |
| 10 | + String content = strs[i].substring(startIdx, strs[i].length()); |
| 11 | + map.computeIfAbsent(content, k -> new ArrayList<>()).add(filePath + "/" + fileName); |
| 12 | + } |
40 | 13 | }
|
| 14 | + List<List<String>> duplicateFiles = new ArrayList<>(); |
| 15 | + for (String key : map.keySet()) { |
| 16 | + if (map.get(key).size() > 1) { |
| 17 | + duplicateFiles.add(map.get(key)); |
| 18 | + } |
| 19 | + } |
| 20 | + return duplicateFiles; |
| 21 | + } |
41 | 22 | }
|
0 commit comments