package a609; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Solution { /* * 输入: ["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", * "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"] 输出: * [["root/a/2.txt","root/c/d/4.txt","root/4.txt"],["root/a/1.txt", * "root/c/3.txt"]] */ public List> findDuplicate(String[] paths) { List fileList = converArr(paths); List> result = new ArrayList>(); Map> map = new HashMap>(); for(String path:fileList) { String content = getContent(path); List files = new ArrayList<>(); if(map.containsKey(content)) { files = map.get(content); } files.add(getFile(path)); map.put(content, files); } for(String key:map.keySet()) { List files = map.get(key); if(files.size()>1) { result.add(files); } } return result; } private static List converArr(String[] paths){ List result = new ArrayList<>(); for(String path:paths) { String[] tmp = path.split(" "); for(int i=1;i