[Leetcode] [JAVA] 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.

Use the two pointer start and end to intercept the string in S, starting with the S head. The end pointer traverses backward until all the characters in T appear in the substring or to the end of S, then move the start backward, try to make the substring length small, until the next move will not satisfy the characters in the T in the substring of the condition or reach end, this time record start and end and calculate the substring length, Update start and End If it is smaller than before. So repeat these two steps until end to the end of S.

The problem is two key moments: 1. All characters in t appear in substrings; 2. Moving backwards will not satisfy the condition that the characters in the T are in the substring, how to be captured.

This can be achieved with two hashmap. The first HashMap, need records the count of the times of all the characters in T, as standard in traversing s, does not change. The second HashMap, found records the dynamic changes in the number of occurrences of a character during traversal.

Condition 2 can be implemented: once the current character in found is found to be equal to the value in need, the traversal of the start pointer is stopped. Otherwise you can continue traversing but the value in found is-1.

Condition 1 can be achieved by contacting the length of T: Use the variable count to record the number of characters in t that have been found in S. Once count equals t the length, start to move back. So, what kind of character is the character in T that has been found in s and can be counted into count? Still using found and need can be judged, if the value of the character in the found is less than or equal to its value in need, the character that is traversed should be counted in count, because there is no or just enough number of times for this character to appear, and it should be counted as the character in T. And if the value in found is greater than the value in need, it means that it has traversed enough of that character, no need to come up with the character, it needs to be the other characters in T, so it cannot be counted into count.

1      Publicstring Minwindow (String S, String T) {2         if(T.length () >s.length ())3             return"";4Hashmap<character, integer> need =NewHashmap<character, integer>();5Hashmap<character, integer> found =NewHashmap<character, integer>();6         7          for(intI=0;i<t.length (); i++) {8             Chart =T.charat (i);9             if(!Need.containskey (t)) {TenNeed.put (t,1); OneFound.put (t,0); A}Else -Need.put (T, Need.get (t) +1); -         } the         intStart = 0; -         intEnd = 0; -         intMinstart =-1; -         intMinend =s.length (); +         intCount = 0; -          for(; End<s.length (); end++) { +             Chart =S.charat (end); A             if(Need.containskey (t)) { atFound.put (T, Found.get (t) +1); -                 if(Found.get (t) <=Need.get (t)) -count++; -                 if(count==t.length ()) { -                      for(; start<=end;start++) { -                         Chart2 =S.charat (start); in                         if(Need.containskey (T2)) { -                             if(Found.get (T2) >need.get (T2)) toFound.put (T2, Found.get (T2)-1); +                             Else -                                  Break; the                         } *                     } $                     if(start>end)Panax Notoginsengstart--; -                     if(end-start<minend-Minstart) { theMinstart =start; +Minend =end; A                     } the                 } +             } -         } $         returnMinstart==-1? ": S.substring (minstart,minend+1); $}

[Leetcode] [JAVA] 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.