File tree 2 files changed +45
-0
lines changed
2 files changed +45
-0
lines changed Original file line number Diff line number Diff line change 81
81
73|[ Set Matrix Zeroes] ( ./0073-set-matrix-zeroes.js ) |Medium|
82
82
74|[ Search a 2D Matrix] ( ./0074-search-a-2d-matrix.js ) |Medium|
83
83
75|[ Sort Colors] ( ./0075-sort-colors.js ) |Medium|
84
+ 76|[ Minimum Window Substring] ( ./0076-minimum-window-substring.js ) |Hard|
84
85
77|[ Combinations] ( ./0077-combinations.js ) |Medium|
85
86
78|[ Subsets] ( ./0078-subsets.js ) |Medium|
86
87
79|[ Word Search] ( ./0079-word-search.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 76. Minimum Window Substring
3
+ * https://leetcode.com/problems/minimum-window-substring/
4
+ * Difficulty: Hard
5
+ *
6
+ * Given two strings s and t of lengths m and n respectively, return the minimum window substring
7
+ * of s such that every character in t (including duplicates) is included in the window. If there
8
+ * is no such substring, return the empty string "".
9
+ *
10
+ * The testcases will be generated such that the answer is unique.
11
+ */
12
+
13
+ /**
14
+ * @param {string } s
15
+ * @param {string } t
16
+ * @return {string }
17
+ */
18
+ var minWindow = function ( s , t ) {
19
+ const values = new Array ( 128 ) . fill ( 0 ) ;
20
+ let [ start , end ] = [ - Infinity , Infinity ] ;
21
+
22
+ for ( let i = 0 ; i < t . length ; i ++ ) {
23
+ values [ t . charCodeAt ( i ) ] ++ ;
24
+ }
25
+
26
+ for ( let i = 0 , j = 0 , total = t . length ; i < s . length ; i ++ ) {
27
+ if ( values [ s . charCodeAt ( i ) ] > 0 ) {
28
+ total -- ;
29
+ }
30
+ values [ s . charCodeAt ( i ) ] -- ;
31
+ while ( ! total ) {
32
+ if ( end - start > i - j ) {
33
+ [ start , end ] = [ j , i ] ;
34
+ }
35
+ values [ s . charCodeAt ( j ) ] ++ ;
36
+ if ( values [ s . charCodeAt ( j ) ] > 0 ) {
37
+ total ++ ;
38
+ }
39
+ j ++ ;
40
+ }
41
+ }
42
+
43
+ return end !== Infinity ? s . slice ( start , end + 1 ) : '' ;
44
+ } ;
You can’t perform that action at this time.
0 commit comments