|
1 | 1 | class Solution {
|
2 | 2 | public List<List<String>> findDuplicate(String[] paths) {
|
3 |
| - Map<String, List<String>> map = new HashMap<>(); |
| 3 | + Map<String, Set<String>> map = new HashMap<>(); |
4 | 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); |
| 5 | + String[] splitPath = path.split("\\s+"); |
| 6 | + String directory = splitPath[0]; |
| 7 | + for (int i = 1; i < splitPath.length; i++) { |
| 8 | + String fileName = splitPath[i].substring(0, splitPath[i].indexOf('(')); |
| 9 | + String fileContent = splitPath[i] |
| 10 | + .substring(splitPath[i].indexOf('(') + 1, splitPath[i].indexOf(')')); |
| 11 | + map.computeIfAbsent(fileContent, k -> new HashSet<>()).add(directory + "/" + fileName); |
12 | 12 | }
|
13 | 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; |
| 14 | + return map.values().stream().filter(entry -> entry.size() > 1).map(ArrayList::new) |
| 15 | + .collect(Collectors.toList()); |
21 | 16 | }
|
22 | 17 | }
|
0 commit comments