LeetCode -- Minimum Window Substring

Source: Internet
Author: User

LeetCode -- Minimum Window Substring
Description:
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 empty string.


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


This question is to find a string in string S [I... k], where I, k in [0, n), n is the length of S, making S [I... k] contains all characters in string T.




Ideas:
1. Use hashT to save the number of occurrences of each character
2. initialize hashS (add each character of t to hashS and initialize the value to 0, that is, the number of occurrences is 0), traverse one s (I, [0, n )), for s [I]:
2.1 If s [I] appears in hashT, accumulate hashS [s [I] to check whether hashS [s [I] and hashT [s [I] are equal, this determines whether the ing of s [I] is completed at this time. If the s ed variable is equal, the mapped variable (initialized to 0) ++
2.2 When all characters in t are mapped, that is, the number of characters in mapped = t (excluding duplicates:
Judging from the leftmost left (initialized to 0) of the current window:
If hashT does not contain s [left]: Direct left ++
If hashT contains s [left] And hashS [s [left] is greater than hashT [s [left], note that the ing quantity of s [left] exceeds the required number hashT [s [left]. You can also set left ++.
In this case, determine whether the distance from left to I is smaller than the current minimum window. If the distance from left to I is smaller than the current minimum window, update the window.




For the implementation of this question, see the following link:
Https://github.com/yuzhangcmu/LeetCode/blob/master/string/MinWindow.java
Http://www.programcreek.com/2014/05/leetcode-minimum-window-substring-java/




Implementation Code:



public class Solution {    public string MinWindow(string s, string t)     {        // 1. save t[i] into hash and get count of unique char in t    var countT = 0;    var hashT = new Dictionary
 
  ();    for(var i = 0;i < t.Length; i++){    if(!hashT.ContainsKey(t[i])){    hashT.Add(t[i], 1);    countT++;    }    else{    hashT[t[i]]++;    }    }        // 2. init hashS     var hashS = new Dictionary
  
   ();    for(var i = 0;i < s.Length; i++){    if(!hashS.ContainsKey(s[i])){    hashS.Add(s[i], 0);    }    }        var mapped = 0; // without duplicate (say 'bccd' , mapped here means 'bcd')    var left = 0;    var minLen = s.Length;    var result = s;        var found = false;    for (int i = 0; i < s.Length; i++) {    char c = s[i];    if (hashT.ContainsKey(c)) {    hashS[c]++;    // we have done mapping for s[i], increase mapped count for it    if (hashT[c] == hashS[c]) {    mapped++;     }    }        if (mapped == countT)     {    found = true;    var leftC = s[left];    // if first char(say c) is not include in t , or count of c in hashS is more than in hashT then: hashS[c] --     // set left++                while (!hashT.ContainsKey(leftC) || hashS[leftC] > hashT[leftC]){                    if (hashT.ContainsKey(leftC) && hashS[leftC] > hashT[leftC]){    hashS[leftC]--;    }                                            left++;                    leftC = s[left];                }                     if (i - left + 1< minLen) {                    result = s.Substring(left, i - left + 1);                    minLen = i - left + 1;                }    }        }        return !found ?  : result;    }}
  
 


 

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.