Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions solution/0000-0099/0076.Minimum Window Substring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

## 解法

滑动窗口

<!-- 这里可写通用的实现逻辑 -->

<!-- tabs:start -->
Expand All @@ -60,6 +62,46 @@

```

### **TypeScript**

```ts
function minWindow(s: string, t: string): string {
let n1 = s.length, n2 = t.length;
if (n1 < n2) return '';
let need = new Array(128).fill(0);
let window = new Array(128).fill(0);
for (let i = 0; i < n2; ++i) {
++need[t.charCodeAt(i)];
}

let left = 0, right = 0;
let res = '';
let count = 0;
let min = n1 + 1;
while (right < n1) {
let cur = s.charCodeAt(right);
++window[cur];
if (need[cur] > 0 && need[cur] >= window[cur]) {
++count;
}
while (count == n2) {
cur = s.charCodeAt(left);
if (need[cur] > 0 && need[cur] >= window[cur]) {
--count;
}
if (right - left + 1 < min) {
min = right - left + 1;
res = s.slice(left, right + 1);
}
--window[cur];
++left;
}
++right;
}
return res;
};
```

### **...**

```
Expand Down
40 changes: 40 additions & 0 deletions solution/0000-0099/0076.Minimum Window Substring/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@

```

### **TypeScript**

```ts
function minWindow(s: string, t: string): string {
let n1 = s.length, n2 = t.length;
if (n1 < n2) return '';
let need = new Array(128).fill(0);
let window = new Array(128).fill(0);
for (let i = 0; i < n2; ++i) {
++need[t.charCodeAt(i)];
}

let left = 0, right = 0;
let res = '';
let count = 0;
let min = n1 + 1;
while (right < n1) {
let cur = s.charCodeAt(right);
++window[cur];
if (need[cur] > 0 && need[cur] >= window[cur]) {
++count;
}
while (count == n2) {
cur = s.charCodeAt(left);
if (need[cur] > 0 && need[cur] >= window[cur]) {
--count;
}
if (right - left + 1 < min) {
min = right - left + 1;
res = s.slice(left, right + 1);
}
--window[cur];
++left;
}
++right;
}
return res;
};
```

### **...**

```
Expand Down
35 changes: 35 additions & 0 deletions solution/0000-0099/0076.Minimum Window Substring/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function minWindow(s: string, t: string): string {
let n1 = s.length, n2 = t.length;
if (n1 < n2) return '';
let need = new Array(128).fill(0);
let window = new Array(128).fill(0);
for (let i = 0; i < n2; ++i) {
++need[t.charCodeAt(i)];
}

let left = 0, right = 0;
let res = '';
let count = 0;
let min = n1 + 1;
while (right < n1) {
let cur = s.charCodeAt(right);
++window[cur];
if (need[cur] > 0 && need[cur] >= window[cur]) {
++count;
}
while (count == n2) {
cur = s.charCodeAt(left);
if (need[cur] > 0 && need[cur] >= window[cur]) {
--count;
}
if (right - left + 1 < min) {
min = right - left + 1;
res = s.slice(left, right + 1);
}
--window[cur];
++left;
}
++right;
}
return res;
};