Leetcode 76.Minimum window Substring (minimum window substring) thinking and method of solving problems

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.

Idea: The problem is to solve the minimum containing t substring, the optimal solution is O (n) time complexity. The idea is to save the number of characters for s and T by using two 128 int arrays.

Start,end the two-pointer, the end++, when the S.charat (end) corresponding int array value +1,start++, will be S.charat (start) corresponding to the int array value -1.

When the end is found to meet the requirements, compared with the minimum length, smaller than the minimum length, the value is exchanged, otherwise, start jumps to the appropriate position, and s.charat (start) corresponds to the array -1.

The specific code is as follows:

public class Solution {public String Minwindow (string s, String t) {char[] CS = S.tochararray ();//Convert an array, and later manipulate the other side        Quick char[] CT = T.tochararray (); if (cs.length ==0 | | ct.length==0) {return "";//if Empty, return null}//define matching string minimum match length int len = Inte Ger.                Max_value; int[] SA = new int[128];//stores the number of characters in the S string int[] TA = new int[128];//Stores the number of T characters//the string to be compared is recorded in the array for (int i = 0; i < ct.length;        i++) {Ta[t.charat (i)]++;                } int start = 0;//start int end = 0;//end int s0 = 0;//Save Minimum length start value int e0 = 0;//Save Minimum Length end value int it = 0;//t string start boolean isendadd = true;//end pointer time change//traverse while (end < Cs.length        {//system.out.println ("Start:" + start);        if (Isendadd)//only end+1 add sa[cs[end]]++; if (End-start >= ct.length-1) {//Only the length of the character greater than T is compared to the current position of the while (it < ct.length) {//t character if (Ta[ct[it]] <= Sa[ct[it]]) {//if one characterThe number is equal, compare the next character it++; }else{break;//if not equal, direct end loop} if (it = = Ct.length) {//compliance required if (Len > end-start + 1)        {//If the value is less than the current S0 = start;//Update start value E0 = end+1;                Len = e0-s0;//Update length}else{//If Len length is smaller than the current length, the current start is directly to the beginning of the Len width, i.e. strat = End-len;            for (; start <= end-len;start++) {sa[cs[start]]--;//The number of characters in the SA array at the same time-1}}            sa[cs[start]]--;        start++;//is less than the current length of 1 Isendadd = false;    it = 0;//after re-match}else{Isendadd = true;    end++;        }}else{Isendadd = true;        end++;    }} return S.substring (S0,E0); }}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode 76.Minimum window Substring (minimum window substring) thinking and method of solving problems

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.