Skip to content

Commit 06c35e4

Browse files
committedJan 23, 2025
Add solution #76
1 parent ef337ed commit 06c35e4

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
73|[Set Matrix Zeroes](./0073-set-matrix-zeroes.js)|Medium|
8282
74|[Search a 2D Matrix](./0074-search-a-2d-matrix.js)|Medium|
8383
75|[Sort Colors](./0075-sort-colors.js)|Medium|
84+
76|[Minimum Window Substring](./0076-minimum-window-substring.js)|Hard|
8485
77|[Combinations](./0077-combinations.js)|Medium|
8586
78|[Subsets](./0078-subsets.js)|Medium|
8687
79|[Word Search](./0079-word-search.js)|Medium|
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.