20 lines concise java implementation


  • 0
    K
    public class Solution {
        public String minWindow(String s, String t) {
            int slen=s.length(),tlen=t.length();
            if(slen<tlen || slen==0 || tlen==0) return "";
            int[] count= new int[256];
            for(char c: t.toCharArray()) count[c]++;
            
            int start=0,end=0;
            int size=0;
            String res="";
            while(end<slen){
               if(count[s.charAt(end++)]-->0)
                   size++;     
                while(size==tlen){
                    if(res=="" || res.length()>end-start) res= s.substring(start,end);
                    if(count[s.charAt(start++)]++==0) size--;   
                }
            }
            return res;
            }
        }
    

Log in to reply
 

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