Time Limit Exceeded!!


  • 0
    S
    public class Solution {
        public List<List<String>> findDuplicate(String[] paths) {
            String pathPrev = "";
            String pathFinal = "";
            String path = "";
            String startNode = "";
            String value = "";
            List<String> list = new ArrayList<String>();
            List<List<String>> dictPath = new ArrayList<>();
            HashMap <String, String> hm = new HashMap<String, String>();
            for(int i=0;i<paths.length;i++){
                paths[i]=paths[i].replace("("," ");
                paths[i]=paths[i].replace(")","");
                String values[] = paths[i].split(" ");
                for(int j=0;j<values.length;j++){
                   // System.out.println("Value is: "+values[j]);
                if(values[j].contains("root"))
                {
                    startNode = values[j]+"/";
                }
                    else if(values[j].contains(".")){
                        path="";
                        path+=startNode+values[j];
                    }
                    else{
                        if(hm.containsKey(values[j]))
                        {
                            pathPrev = hm.get(values[j]);
                            pathFinal = pathPrev+","+path;
                            hm.put(values[j],pathFinal);
                            
                        }
                        else{
                        value = values[j];
                        hm.put(value,path);
                        }
                    }
                }
                
                
            }
        for(Map.Entry<String, String> entry : hm.entrySet()) {
            if(hm.containsKey(entry.getKey()))
            {
                List<String> items = Arrays.asList(entry.getValue().split("\\s*,\\s*"));
                if(items.size()>1)
                dictPath.add(items);
            }
    }
            return dictPath;
        }
    }

  • 0
    S

    @shikha.kukreja said in Time Limit Exceeded!!:

    public class Solution {
        public List<List<String>> findDuplicate(String[] paths) {
            String pathPrev = "";
            String pathFinal = "";
            String path = "";
            String startNode = "";
            String value = "";
            List<String> list = new ArrayList<String>();
            List<List<String>> dictPath = new ArrayList<>();
            HashMap <String, String> hm = new HashMap<String, String>();
            for(int i=0;i<paths.length;i++){
                paths[i]=paths[i].replace("("," ");
                paths[i]=paths[i].replace(")","");
                String values[] = paths[i].split(" ");
                for(int j=0;j<values.length;j++){
                   // System.out.println("Value is: "+values[j]);
                if(values[j].contains("root"))
                {
                    startNode = values[j]+"/";
                }
                    else if(values[j].contains(".")){
                        path="";
                        path+=startNode+values[j];
                    }
                    else{
                        if(hm.containsKey(values[j]))
                        {
                            pathPrev = hm.get(values[j]);
                            pathFinal = pathPrev+","+path;
                            hm.put(values[j],pathFinal);
                            
                        }
                        else{
                        value = values[j];
                        hm.put(value,path);
                        }
                    }
                }
                
                
            }
        for(Map.Entry<String, String> entry : hm.entrySet()) {
            if(hm.containsKey(entry.getKey()))
            {
                List<String> items = Arrays.asList(entry.getValue().split("\\s*,\\s*"));
                if(items.size()>1)
                dictPath.add(items);
            }
    }
            return dictPath;
        }
    }
    

    Can you guys tell me, what can I do to make it pass all the 169 test cases?


Log in to reply
 

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