Java Easy to understand Solution


  • 0
    L
    class Solution {
        public String minWindow(String s, String t) {
            if(t.length()>s.length()) return "";
            
            int Window=t.length(), count=0, i=0, j=0, start=0, minStart=-1, end=-1, minLength=Integer.MAX_VALUE;
            int[] sMap = new int[256], tMap = new int[256];
            
            for(i=0;i<t.length();i++)
                tMap[t.charAt(i)]++;
            
            i=0;
            while(i<s.length())
            {
                if(sMap[s.charAt(i)]<tMap[s.charAt(i)])
                {
                    count++;
                }
                sMap[s.charAt(i)]++;
                i++;
                if(count==t.length())
                {
                   while(start<s.length()-t.length()+1)
                    {
                        if(sMap[s.charAt(start)]>tMap[s.charAt(start)])
                        {
                            sMap[s.charAt(start++)]--;
                        }
                        else
                            break;
                    }
                    if(minLength>i-start)
                    {
                        minLength = i-start;
                        end=i;
                        minStart=start;
                    }
                }
            }
            if (end==-1) return "";
            return s.substring(minStart, end);
        }
    }
    

Log in to reply
 

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