File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * https://leetcode.com/problems/minimum-window-substring
3+ * T.C. O(s + t)
4+ * S.C. O(t)
5+ */
6+ function minWindow ( s : string , t : string ) : string {
7+ let minLow = 0 ;
8+ let minHigh = s . length ;
9+
10+ const counts : Record < string , number > = { } ;
11+ for ( const c of t ) {
12+ counts [ c ] = ( counts [ c ] || 0 ) + 1 ;
13+ }
14+
15+ let included = 0 ;
16+
17+ let low = 0 ;
18+ for ( let high = 0 ; high < s . length ; high ++ ) {
19+ if ( counts [ s [ high ] ] ) {
20+ if ( counts [ s [ high ] ] > 0 ) {
21+ included ++ ;
22+ }
23+ counts [ s [ high ] ] -- ;
24+ }
25+
26+ while ( included === t . length ) {
27+ if ( high - low < minHigh - minLow ) {
28+ minLow = low ;
29+ minHigh = high ;
30+ }
31+
32+ if ( counts [ s [ low ] ] ) {
33+ counts [ s [ low ] ] ++ ;
34+ if ( counts [ s [ low ] ] > 0 ) {
35+ included -- ;
36+ }
37+ }
38+
39+ low ++ ;
40+ }
41+ }
42+
43+ return minHigh === s . length ? '' : s . substring ( minLow , minHigh + 1 ) ;
44+ }
You can’t perform that action at this time.
0 commit comments