Leetcode Minimum Window Substring-----java

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.

On the premise that the time complexity is O (n), the smallest string containing all the letters of T in S is calculated.

For the first time, use a 256-size array to record the letters in T. It then finds each qualifying string, compares the size, and finally expires.

 Public classSolution { Publicstring Minwindow (string s, String t) {intLen1 = S.length (), len2 =t.length (); int[] TT =New int[256];  for(inti = 0;i<len2;i++) Tt[t.charat (i)]++; int[] t2 =Tt.clone (); intnum = 0,start = 0,end = 0,length = 0; intmore =len1;  for(inti = 0;i<len1;i++){            if(T2[s.charat (i)] > 0) { more= Length = = 0?len1:length-Len2;  for(intj = i;j<len1;j++){                    if(T2[s.charat (j)] > 0) {num++; T2[s.charat (j)]--; }Else{ More--; if(more = = 0)                             Break; }                    if(num = =len2) {                        if(length = = 0 | | (j-i+1) <length) {Start=i; End=J; Length= End-start+1; more= Length-Len2; } T2=Tt.clone (); Num= 0;  Break; }} T2=Tt.clone (); Num= 0; }            if(Length = =len2) Break; }        if(length = = 0 )            return""; Char[] result =New Char[length];  for(inti = 0;i<length;i++) Result[i]= S.charat (i+start); returnstring.valueof (Result); }}

And then, to improve, the two record points left,right all from left to right, the letter is recorded in the LOC array each time it is walked, and if all letters are included in this (left-right.

The speed is still possible.

 Public classSolution { Public BooleanCoverint[] A,int[] b) {         for(inti = 0;i<256;i++)            if(A[i] <B[i])return false; return true; }         Publicstring Minwindow (string s, String t) {intLen1 =s.length (); intLen2 =t.length (); int[] pos =New int[256];  for(inti = 0;i<len2;i++) Pos[t.charat (i)]++; int[] loc =New int[256]; intMin = Len1+1,start = 0,end = 0;  for(intleft = 0,right = 0;right < len1;right++) {Loc[s.charat (right)]++; if( !cover (Loc,pos))Continue;  while(Left <=Right ) {                CharCH =S.charat (left); if(Pos[ch] = =Loc[ch]) Break; LOC[CH]--; Left++; }            if(Right-left <min) {min= right-Left ; Start=Left ; End=Right ; }        }        if(min = = len1+1)            return""; Char[] result =New Char[Min+1];  for(inti = Start,j = 0;i<=end;i++,j++) Result[j]=S.charat (i); returnstring.valueof (Result); }}

Once you've found that you've wasted a lot of time cover each time, add the NUM variable and record the number of letters that have been hit between the current left and right. This speed has been further improved.

 Public classSolution { Publicstring Minwindow (string s, String t) {intLen1 =s.length (); intLen2 =t.length (); if(Len1 = = 0 | | len2 = = 0)            return""; int[] pos =New int[256];  for(inti = 0;i<len2;i++) Pos[t.charat (i)]++; int[] loc =New int[256]; intMin = Len1+1,start = 0,end = 0,num = 0; intleft = 0,right = 0;  while(Right <len1) {            if(Num <len2) {                CharCH =S.charat (right); if(Pos[ch] > 0) {Loc[ch]++; if(Loc[ch] <=pos[ch]) num++; } Right++; }             while(num = =len2) {                if(Min > (right-Left )) {min= right-Left ; Start=Left ; End=Right ; }                CharCH =S.charat (left); if(Loc[ch] = = 0) Left++; Else if(Loc[ch] >Pos[ch]) { Left++; LOC[CH]--; }Else{num--; Left++; LOC[CH]--; }            }        }        if(min = = len1+1)            return""; Char[] result =New Char[min];  for(inti = Start,j = 0;i<end;i++,j++) Result[j]=S.charat (i); returnstring.valueof (Result); }}

You can also do a bit of optimization, that is, to convert a string into char[] so in the number of times, there will be a higher speed, will basically reach the fastest

Just add char[] S_array = S.tochararray () and Change S.charat (i) to s_array[i].

Code changes are not big, it is not uploaded once.

Leetcode Minimum Window Substring-----java

Related Article

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.