Slower but simple solution using a hash table in Java


  • 0
    L
         public List<List<String>> findDuplicate(String[] paths) {
            Map<String,List<String>> map = new HashMap<>();
            for(String path : paths) {
                decodeAndRegister(path,map);
            }
            return map.values().parallelStream().filter((e)->e.size()>1).collect(Collectors.toList());
        }
        
        //"root/a 1.txt(abcd) 2.txt(efgh)"
        private static void decodeAndRegister(String s, 
            Map<String,List<String>> map) {
            String[] sub = s.split(" ");
            String base = sub[0];
            for(int i=1;i<sub.length;i++) {
                String val = sub[i];
                String content = val.substring(val.indexOf('('));
                StringBuilder builder = new StringBuilder(base);
                builder.append('/');
                builder.append(val.substring(0,val.indexOf('(')));
                List<String> list = map.get(content);
                if(list==null) {
                    list = new ArrayList<>();
                    map.put(content,list);
                }
                list.add(builder.toString());
            }
        }
    
    

Log in to reply
 

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