天题系列 Minimum Window Substring

压力太大了,这道题先不做了

public class Solution {

    public String minWindow(String S, String T) {

        // 讲解http://articles.leetcode.com/2010/11/finding-minimum-window-in-s-which.html

        //代码 http://www.cnblogs.com/huntfor/p/3915288.html

        String res = "";

        if(S==null || T==null) return "";

        

        int[]  found= new int[256];

        int[] needfind = new int[256];

        int minLen = S.length()+1;

        

        for(int i=0;i<T.length();i++){

            needfind[T.charAt(i)]++;

        }

        int left = 0, cnt =0;

        

        for(int i=0;i<S.length(); i++){ // i is right end

            if(needfind[S.charAt(i)]==0) continue;  // skip

            

            found[S.charAt(i)]++;

            if(found[S.charAt(i)] <= needfind[S.charAt(i)])

                cnt++;

                

            // while restraint reachs

            if(cnt==T.length()){

                while(left<S.length() &&(needfind[S.charAt(left)] ==0 || found[S.charAt(left)] >needfind[S.charAt(left)]) ){

                    if(found[S.charAt(left)] >needfind[S.charAt(left)])

                        found[S.charAt(left)]--;

                    

                    left++;

                }

                

                int winlen = i-left+1;

                if(winlen<minLen){

                    minLen = winlen;

                    res = S.substring(left, i+1);

                }

            }

        }

        

        return res;

    }

}

总有一天我们回首曾经的痛苦和迷茫,会觉得那算个什么事儿

 

你可能感兴趣的:(substring)