Leetcode 76. 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, this covers all characters in T, return the empty string "" .

If There is multiple such windows, you is guaranteed that there would always being only one unique minimum window in S.

We start by scanning T first, putting the corresponding characters and the number of occurrences into the hash table.

Then we begin to traverse S and encounter the characters in T, and subtract the value from the corresponding hash table until it contains all the characters in T, record a string and update the minimum string value.

Move the left edge of the child window to the right, skipping the characters that are not in T, or you can skip the character if the number of characters in T appears greater than the value in the hash table.

Code:

public class Solution {public string Minwindow (string s, String t) {hashmap<character,integer> TMap = NE        W hashmap<character,integer> ();        char[] tc = T.tochararray ();                for (int i=0;i<tc.length;i++) {//T to array, each letter is placed in HashMap as a key value, and the number of occurrences is its value if (Tmap.containskey (Tc[i])) {                          Tmap.put (Tc[i],tmap.get (Tc[i]) +1);            } else{Tmap.put (tc[i],1);        }} int left = 0,scond = 0;        int count = 0;        String ans = "";        int minlen = s.length () +1;        char []SC = S.tochararray ();  for (int right = 0;right<s.length (); right++) {char rc = sc[right];                  Right corresponds to the string if (Tmap.containskey (RC)) {Tmap.put (Rc,tmap.get (RC)-1);//The letter appears once in the window, value values--                if (Tmap.get (RC) >=0)//When the number of occurrences in the window is less than the number of occurrences in t =, the description is not more than count++; while (count = = T.length ()) {//When count equals t length, do two things, a left-right move, skipping characters that do not appear in T, or if (Right-left+1 < Minlen) {//two: Judging and updating the minimum window value and ans, or more occurrences than t                        Ans = s.substring (left,right+1);                    Minlen = right-left+1; } char lc = Sc[left];                        The left corresponding string if (Tmap.containskey (LC)) {Tmap.put (Lc,tmap.get (LC) +1);                    if (Tmap.get (LC) >0) count--;                } left++;    }}} return ans; }}

Leetcode 76. 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.