Java solution


  • 0
    U
    public class Solution {
        public List<List<String>> findDuplicate(String[] paths) {
            
            Map<String, ArrayList<String>> map = new HashMap<>();
            
            for (String folder : paths) {
                String[] elements = folder.split(" ");
                String path = elements[0];
                for (int i = 1; i < elements.length; i++) {
                    String content = getContent(elements[i]);
                    String filePath = buildFilePath(path, elements[i]);
                    if (map.containsKey(content)) {
                        map.get(content).add(filePath);
                    } else {
                        ArrayList<String> list = new ArrayList<>();
                        list.add(filePath);
                        map.put(content, list);
                    }
                }
            }
            
            List<List<String>> res = new ArrayList<>();
            for (List<String> list : map.values()) {
                if (list.size() > 1) {
                    res.add(list);
                }
            }
            return res;
        }
        
        private static String getContent(String file) {
            return file.substring(file.indexOf("(") + 1, file.indexOf(")"));
        }
        
        private static String buildFilePath(String root, String fileName) {
            return root + "/" + fileName.substring(0, fileName.indexOf("("));
        }
    }
    
    

Log in to reply
 

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