Leetcode-minimum Window Substring

Source: Internet
Author: User

Given a string S and a string T, find the minimum window in S which would 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 so covers all characters in T, return the emtpy string "" .

If There is multiple such windows, you is guaranteed that there would always being 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's 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 so at current position, the number of this char was more than needed . We also record the position of each char in S so belongs to T. Everytime a char is enqueued, we check the head of the Qu Eue. 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 classSolution {2      Publicstring Minwindow (String S, String T) {3         if(S.length () ==0 | | T.length () ==0)return"";4         5Map<character,integer> mapt =NewHashmap<character,integer>();6          for(intI=0;i<t.length (); i++){7             Charc =T.charat (i);8             if(Mapt.containskey (c))9Mapt.put (C,mapt.get (c) +1);Ten             ElseMapt.put (c,1); One         } A  -List<integer> indexlist =NewLinkedlist<integer>(); -         intleft =t.length (); the         intStart =-1, end =-1; -         intCurlen =Integer.max_value; -          for(intI=0;i<s.length (); i++){ -             Charc =S.charat (i); +             if(!mapt.containskey (c))Continue; -  +             intnum =Mapt.get (c); A             if(num>0) left--; atMapt.put (c,num-1); - Indexlist.add (i); -  -             CharHead = S.charat (indexlist.get (0)); -              while(Mapt.get (head) <0){ -Indexlist.remove (0); inMapt.put (Head,mapt.get (head) +1); -Head = S.charat (indexlist.get (0)); to             } +  -             if(left==0){ the                 intNewlen = Indexlist.get (Indexlist.size ()-1)-indexlist.get (0) +1; *                 if(newlen<Curlen) { $Start = Indexlist.get (0);Panax NotoginsengEnd = Indexlist.get (Indexlist.size ()-1); -Curlen =Newlen; the                 } +              } A          } the  +          if(Curlen==integer.max_value)return""; -          Else { $String res = s.substring (start,end+1); $              returnRes;  -          } -     } the}

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 if perform QUERY operation, i.e., check the element on position N.

Leetcode-minimum Window Substring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.