Time Limit Exceeded! Can anyone please tell the reason?


  • 0
    H
    public List<Integer> findAnagrams(String s, String p) {
            List<Integer> li = new ArrayList<>();
            HashMap<Character,Integer> map = new HashMap<>();
            for(char c:p.toCharArray()){
                if(map.containsKey(c)){
                    map.put(c,map.get(c) + 1);
                }else{
                map.put(c,1);
                }
            }
            int l = p.length();
            int c=0;
            for(int i=0;i<=s.length()-l;i++){
                if( isAnagram ((s.substring(i,i+l)),p, map) == true ){
                    li.add(i);
                }
            }
            return li;
            
        }
        public boolean isAnagram(String a,String p, HashMap<Character,Integer> map){
          
            HashMap<Character,Integer> map_1 = new HashMap<>();
            for(char c:a.toCharArray()){
                if(map_1.containsKey(c)){
                    map_1.put(c,map_1.get(c) + 1);
                }else{
                    map_1.put(c,1);
                }
            }
            for(char c:p.toCharArray()){
                if(map.get(c) != map_1.get(c)){
                    return false;
                }
            }
                
            return true;
        }
    

Log in to reply
 

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