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.
Problem Solving Ideas:
Involves a find operation, puts T into TMap, and creates a graph SMAP that holds all the characters in T (not duplicated), with a counter representing the degree of overlap between SMAP and TMap, and once SMAP contains TMap, a set of solutions is found, By shrinking the begin pointer to get exactly the position containing the TMap, then the comparison size, the Java implementation is as follows:
public string Minwindow (string s, String t) {hashmap<character, integer> tMap = new Hashmap<character, I Nteger> (); Hashmap<character, integer> sMap = new Hashmap<character, integer> (); for (int i=0;i<t.length (); i++) {Smap.put (T.charat (i), 0); if (!tmap.containskey (T.charat (i))) Tmap.put (T.charat (i), 1); Else Tmap.put (T.charat (i), Tmap.get (T.charat (i)) + 1); } int begin=0,count=0,minbegin=0,length=s.length () +1;; for (int i=0;i<s.length (); i++) {if (!tmap.containskey (S.charat (i))) continue; Smap.put (S.charat (i), Smap.get (S.charat (i)) +1); if (Smap.get (S.charat (i)) <=tmap.get (S.charat (i))) count++; if (Count==t.length ()) {for (int j=begin;j<=i;j++) {if (!tmap.containskey (S.charat (j))) continue; if (Smap.get (S.charat (j)) >tmap.get (S.charat (j))) {Smap.put (S.charat (j), Smap.get (S.charaT (j))-1); Continue } smap.put (S.charat (j), Smap.get (S.charat (j))-1); count--; begin=j+1; if (length>i-j) {length=i-j; Minbegin=j; } break; }}} return Length!=s.length () +1?s.substring (Minbegin, minbegin+length+1): ""; }
Java for Leetcode 076 Minimum Window Substring