首页 > 代码库 > Leetcode-Minimum Window Substring

Leetcode-Minimum Window Substring

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".

Note:
If there is no such window in S that covers all characters in T, return the emtpy string "".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

Analysis:

We use a HashMap to store the number of each char in T. We scan the string S from 0 to its end. For each char, if it is one of the char in T, we decrease the count of this char in the Map. If the count of the char is smaller than 0, it means that at current position, the number of this char is more than needed. We also record the position of each char in S that belongs to T. Everytime a char is enqueued, we check the head of the queue. If the char of the head is more than needed, we then dequeue the head and check the next head until the number of the char of the head is less than or equal to 0.

Solution:

 1 public class Solution { 2     public String minWindow(String S, String T) { 3         if (S.length()==0 || T.length()==0) return ""; 4          5         Map<Character,Integer> mapT = new HashMap<Character,Integer>(); 6         for (int i=0;i<T.length();i++){ 7             char c = T.charAt(i); 8             if (mapT.containsKey(c)) 9                 mapT.put(c,mapT.get(c)+1);10             else mapT.put(c,1);11         }12 13         List<Integer> indexList = new LinkedList<Integer>();14         int left = T.length();15         int start = -1, end = -1;16         int curLen = Integer.MAX_VALUE;17         for (int i=0;i<S.length();i++){18             char c = S.charAt(i);19             if (!mapT.containsKey(c)) continue;20 21             int num = mapT.get(c);22             if (num>0) left--;23             mapT.put(c,num-1);24             indexList.add(i);25 26             char head = S.charAt(indexList.get(0));27             while (mapT.get(head)<0){28                 indexList.remove(0);29                 mapT.put(head,mapT.get(head)+1);30                 head = S.charAt(indexList.get(0));31             }32 33             if (left==0){34                 int newLen = indexList.get(indexList.size()-1)-indexList.get(0)+1;35                 if (newLen<curLen){36                     start = indexList.get(0);37                     end = indexList.get(indexList.size()-1);38                     curLen = newLen;39                 }40              }41          }42 43          if (curLen==Integer.MAX_VALUE) return "";44          else {45              String res = S.substring(start,end+1);46              return res;       47          }48     }49 }

NOTE: I used ArrayList at the begnning and got TLE error. After changing to LinkedList, the solution is accepted. Because:

LinkedList is FASTER when perform DELETE and INSERT operation.

ArrayList is FASTER when perform QUERY operation, i.e., check the element on position n.

 

Leetcode-Minimum Window Substring