File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {string } s
3+ * @param {string } t
4+ * @return {string }
5+ */
6+ const minWindow = function ( s , t ) {
7+ const map = { }
8+ for ( const c of t ) {
9+ map [ c ] = ( map [ c ] || 0 ) + 1
10+ }
11+ let counter = t . length
12+ let start = 0
13+ let end = 0
14+ let minLen = Infinity
15+ let minStart = 0
16+ while ( end < s . length ) {
17+ const eChar = s [ end ]
18+ if ( map [ eChar ] > 0 ) {
19+ counter --
20+ }
21+ map [ eChar ] = ( map [ eChar ] || 0 ) - 1
22+ end ++
23+ while ( counter === 0 ) {
24+ if ( end - start < minLen ) {
25+ minStart = start
26+ minLen = end - start
27+ }
28+ const sChar = s [ start ]
29+ map [ sChar ] = ( map [ sChar ] || 0 ) + 1
30+ if ( map [ sChar ] > 0 ) {
31+ counter ++
32+ }
33+ start ++
34+ }
35+ }
36+ if ( minLen !== Infinity ) {
37+ return s . substring ( minStart , minStart + minLen )
38+ }
39+ return ''
40+ }
You can’t perform that action at this time.
0 commit comments