leetcode(30)串联所有单词的子串

串联所有单词的子串

解题思路:滑动窗口以及Hash

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> result = new ArrayList<>();
        if(s.length()==0||words.length==0||words[0].length()==0){
            return result;
        }
        int slen = s.length();
        int wordLen = words.length;
        int itemLen = words[0].length();
        Integer t = null;
        String temps = null;
        int n = 0;
        Map<String,Integer> map = new HashMap<>();
        Map<String,Integer> map2 = new HashMap<>();
        String[] strs = new String[slen-itemLen+1];
        for(int i=0;i<wordLen;i++){
            t = map.get(words[i]);
            if(t==null){
               map.put(words[i],1); 
            }else{
               map.put(words[i],t+1);
            }
        }
        for(int i=0;i<slen-itemLen+1;i++){
            temps = s.substring(i,i+itemLen);
            if(map.containsKey(temps)){
                strs[i] = temps;
            }
        }
        for(int i=0;i<slen-wordLen*itemLen+1;i++){
            map2.clear();
            n=0;
            temps = strs[i+n*itemLen];
            while(temps!=null){ 
                t = map2.get(temps);
                if(t==null){
                    map2.put(temps,1);
                }else{
                    if(t+1>map.get(temps)){
                        break;
                    }
                    map2.put(temps,t+1);
                }
                n++;
                if(n==wordLen){
                    result.add(i);
                    break;
                }
                temps = strs[i+n*itemLen];
            }
        }
        return result;
    }
}

第二种方法:

解题思路:在第一种基础上,不清空Hash表,而是逐渐调整Hash表,这样子会节省大量的时间。

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> result = new ArrayList<>();
        if(s.length()==0||words.length==0||words[0].length()==0){
            return result;
        }
        int slen = s.length();
        int wordLen = words.length;
        int itemLen = words[0].length();
        Integer t = null;
        String temps = null;
        int n = 0;
        int start = 0;
        int index = 0;
        Map<String,Integer> map = new HashMap<>();
        Map<String,Integer> map2 = new HashMap<>();
        String[] strs = new String[slen-itemLen+1];
        for(int i=0;i<wordLen;i++){
            t = map.get(words[i]);
            if(t==null){
               map.put(words[i],1); 
            }else{
               map.put(words[i],t+1);
            }
        }
        for(int i=0;i<slen-itemLen+1;i++){
            temps = s.substring(i,i+itemLen);
            if(map.containsKey(temps)){
                strs[i] = temps;
            }
        }
        for(int i=0;i<itemLen;i++){
            map2.clear();
            n=0;
            start = i;
            while(start+n*itemLen<slen-itemLen+1&&start<slen-wordLen*itemLen+1){
                index = start+n*itemLen;
                if(strs[index]==null){
                    map2.clear();
                    n=0;
                    start = index+itemLen;
                }else{
                    t = map2.get(strs[index]);
                    if(t==null){
                        map2.put(strs[index],1);  
                    }else{
                        if(t+1>map.get(strs[index])){
                            while(!strs[start].equals(strs[index])){
                                t = map2.get(strs[start]);
                                map2.put(strs[start],t-1);  
                                start +=itemLen;
                                n--;
                            }
                            t = map2.get(strs[index]);
                            map2.put(strs[index],t-1);
                            start +=itemLen;
                            n--;
                        }
                        t = map2.get(strs[index]);
                        map2.put(strs[index],t+1);   
                    }
                    n++;
                    if(n==wordLen){
                        result.add(start);
                    }
                }
            }
            
        }
        return result;
    }
}

 

posted @ 2019-06-27 15:55  海平面下的我们  阅读(196)  评论(0编辑  收藏  举报