Short java solution


  • 0
    Y

    public class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
    HashMap<String,Integer > hmap = new HashMap<String,Integer >();
    List<Integer> list= new ArrayList<Integer>();
    if(words==null||words.length==0){
    list.add(0);
    return list;
    }
    if(words[0]==null||words[0].length()==0){
    list.add(0);
    return list;
    }
    for(int i=0;i<words.length;i++){
    if(hmap.containsKey(words[i])){
    hmap.put(words[i],hmap.get(words[i])+1);
    }
    else{
    hmap.put(words[i],1);
    }

         }
         
         int w=words[0].length();
         
         
         HashMap<String,Integer> nhmap = new HashMap<String,Integer >();
         
         for(int i=0;i<=(s.length()-words.length*w);i++){
             int num=0;
             nhmap.putAll(hmap);
             for(int j=i;j<i+words.length*w;j=j+w){
                 String x=s.substring(j,j+w);
                 if(nhmap.get(x)==null){
                     j=i+words.length*w;
                 }
                  else if(nhmap.get(x)==1){
                     nhmap.remove(x);
                     num++;
                 }   
                 else  {
                     nhmap.put(x,nhmap.get(x)-1);
                     num++;
                 }
                 
                 
                 if(num==words.length){
                     list.add(i);
                 }
             }
         }
         
         
         return list;
         
    }
    

    }


Log in to reply
 

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