Simple Java solution using HashMap


  • 0
    A

    '''
    public class Solution {
    public List<List<String>> findDuplicate(String[] paths) {
    List<List<String>> list = new ArrayList<List<String>>();

        HashMap<String,List<String>> map = new HashMap<>();
        
        int l = paths.length;
        
        for(int i=0;i<l;i++){
            String s = paths[i];
            StringBuilder sb = new StringBuilder("");
            char[] stc = s.toCharArray();
            int m = s.length();
            int j = 0;
            while(stc[j] != ' '){
                sb.append(stc[j++]);
            }
            String path = sb.toString();
            sb.setLength(0);
            j++;
            String res = "";
            String name = "";
            while(j < m){
                if(stc[j] == ' '){
                    res = "";
                    name = "";
                }else if(stc[j] == '('){
                    name = path + '/' + res;
                    res = "";
                }else if(stc[j] == ')'){
                    if(map.containsKey(res)){
                        List<String> slist = map.get(res);
                        slist.add(name);
                        map.put(res,slist);
                    }else{
                        List<String> slist = new ArrayList<String>();
                        slist.add(name);
                        map.put(res,slist);
                    }
                }else{
                    res += stc[j];
                }
                j++;
            }
        }
        for(String s : map.keySet()){
            List<String> slist = map.get(s);
            if(slist.size() > 1){
                list.add(slist);
            }
        }
        
        return list;
    }
    

    }
    '''


Log in to reply
 

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