1. 程式人生 > >76. Minimum Window Substring

76. Minimum Window Substring

ring clas win amp logs int integer spa ini

class Solution {
    public String minWindow(String s, String t) {
        boolean[] chs=new boolean[128];
        int[] cnt=new int[128];
        for(int i=0;i<t.length();i++)
        {
            cnt[t.charAt(i)]++;
            chs[t.charAt(i)]=true;
        }
        int count=t.length();
        
int l=0; int r=0; int end=Integer.MAX_VALUE; int start=0; while(r<=s.length()&&l<s.length()) { if(count>0&&r<s.length()) { if(chs[s.charAt(r)]==true&&cnt[s.charAt(r)]>0) count
--; cnt[s.charAt(r)]--; r++; } else if(count<=0) { if(r-l<end-start) { start=l; end=r; } cnt[s.charAt(l)]++;
if(chs[s.charAt(l)]==true&&cnt[s.charAt(l)]>0) count++; l++; } else break; } if(end==Integer.MAX_VALUE) return ""; return s.substring(start, end); } }

76. Minimum Window Substring