Java Hashing Solution


  • 0
    public List<List<String>> findDuplicate(String[] paths) {
        List<List<String>> res = new LinkedList<>();
        Map<String, List<String>> map = new HashMap<>();
        for (String path : paths) {
            String[] split = path.split(" ");
            for (int i=1;i<split.length;i++) {
                String file = split[i].substring(0, split[i].indexOf("("));
                String content = split[i].substring(split[i].indexOf("(")+1, split[i].length()-1);
                map.putIfAbsent(content, new LinkedList<String>());
                map.get(content).add(split[0] + "/" + file);
            }
        }
        for (List<String> list : map.values()) 
            if (list.size() > 1) res.add(list);
        return res;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.