Minimum Window Substring

来源:互联网 发布:淘宝店主真人内衣展示 编辑:程序博客网 时间:2024/06/05 05:44

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).

For example,
S = "ADOBECODEBANC"
T = "ABC"

Minimum window is "BANC".

public String minWindow1(String S, String T) {        if (S.length()==0||T.length()==0||S.length()<T.length()) return "";        int left=T.length(),start=-1,end=S.length();        Deque<Integer> queue= new LinkedList<Integer>();        Map<Character,Integer> map= new HashMap<Character,Integer>();        for (int i=0;i<T.length();i++){            char c= T.charAt(i);            map.put(c,map.containsKey(c)?map.get(c)+1:1);        }        for (int i =0;i<S.length();i++){            char c= S.charAt(i);            if (!map.containsKey(c))                continue;            int n = map.get(c);            map.put(c,n-1);            queue.add(i);            if (n>0) left--;            char head = S.charAt(queue.peek());            while(map.get(head)<0){                queue.poll();                map.put(head,map.get(head)+1);                head=S.charAt(queue.peek());            }            if (left==0){                int new_length=queue.peekLast()-queue.peek()+1;                if (new_length<end-start) {                    start=queue.peek();                    end=queue.peekLast()+1;                }             }        }        if (left==0)  return S.substring(start,end);        else return "";    }

0 0