码迷,mamicode.com
首页 > 其他好文 > 详细

天题系列:Substring with Concatenation of All Words

时间:2015-04-21 08:20:56      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

滑动窗口,但是很繁琐

 public class Solution {
    public ArrayList<Integer> findSubstring(String S, String[] L) {
        //http://www.cnblogs.com/springfor/p/3872516.html
        ArrayList<Integer> res = new ArrayList<Integer>();
        if(S==null || L==null || S.length()==0 || L.length==0 ||L[0].length()==0)  return res;
        HashMap<String, Integer> dict = new HashMap<String, Integer>();
        int wl = L[0].length();
        
        for(String i: L){
            if(dict.containsKey(i)){
                dict.put(i, dict.get(i)+1);
            }else
                dict.put(i, 1);
        }
        
        for(int i=0; i<wl; i++){ 
            int ind = i;  // window‘s starting index
            int cnt = 0;                        // check how many words included in the window, and it should not be more than L‘s length 
            HashMap<String, Integer> curdic = new HashMap<String, Integer>();
          // for(int j = 0; j<S.length()-wl; j+=wl){ // check all words in S
              for(int j = i; j<=S.length()-wl; j+=wl){
                String curwd = S.substring(j, j+wl);
                if(!dict.containsKey(curwd)){ // means there is no such word in L, clear curdic and move on(Concatenation must be continuous),
                    curdic.clear();
                    cnt=0;
                    ind = j+wl;
                }else{
                    if(!curdic.containsKey(curwd)){
                        curdic.put(curwd, 1);
                    }else{
                        curdic.put(curwd,curdic.get(curwd)+1);
                    }
                
                // is the window curdic overflow? now check cnt
                    if(curdic.get(curwd)<=dict.get(curwd)){
                        cnt++;
                    }else{
                         while(curdic.get(curwd)>dict.get(curwd)){ // slide the window and remove the words(tmp) till curwd
                             String tmp = S.substring(ind, ind+wl);
                                curdic.put(tmp, curdic.get(tmp)-1);
                                ind += wl;
                                if(curdic.get(tmp)<dict.get(tmp))
                                    cnt--;
                        }
                    }
                        // is cnt already == L? which means the window has all L‘s words
                    if(cnt==L.length){
                         res.add(ind);
                         String tmp = S.substring(ind, ind+wl);
                         curdic.put(tmp, curdic.get(tmp)-1);
                         ind += wl;
                         cnt--;   // 注意这里cnt的挪动,因为同一个i起始点,window curdic可以挪,cnt是决定是否记录一下这个位置
                    }
                }
            }
        }
        return res;
    }
}
 

 

天题系列:Substring with Concatenation of All Words

标签:

原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4443280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!