easist solution ever! very similiar to substring Sliding window problem!


  • 0
    T
    public class Solution {
        public List<Integer> findSubstring(String s, String[] words) {
            if(s.length()==0||s==null||words.length==0||words==null) return null;
            List<Integer> res = new ArrayList<>();
            Map<String,Integer> hm = new HashMap<>();
            for(String ele:words){
                hm.put(ele,hm.getOrDefault(ele,0)+1);
            }
            int count=words.length;
            
            for(int i=0;i<words[0].length();i++){
                  int start=i;
                  Map<String,Integer> map=new HashMap<>(hm);
                
                  for(int end=start;end+words[0].length()<=s.length();end+=words[0].length()){
                      String str = s.substring(end,end+words[0].length());
                      map.put(str,map.getOrDefault(str,0)-1);
                      
                      while(map.get(str)<0){
                          String temp_str=s.substring(start,start+words[0].length());
                          map.put(temp_str,map.getOrDefault(temp_str,0)+1);
                          start+=words[0].length();
                      }
                      
                      if(end+words[0].length()-start==words[0].length()*words.length){
                          res.add(start);
                      }
                  }
            }
            
            return res;
    }
        
    }
    

Log in to reply
 

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