Skip to content

Commit 74cc0dc

Browse files
authored
feat: add typescript solution to lc problem: No.0076.Minimum Window Substring (doocs#458)
1 parent 953a099 commit 74cc0dc

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

solution/0000-0099/0076.Minimum Window Substring/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
## 解法
4242

43+
滑动窗口
44+
4345
<!-- 这里可写通用的实现逻辑 -->
4446

4547
<!-- tabs:start -->
@@ -60,6 +62,46 @@
6062

6163
```
6264

65+
### **TypeScript**
66+
67+
```ts
68+
function minWindow(s: string, t: string): string {
69+
let n1 = s.length, n2 = t.length;
70+
if (n1 < n2) return '';
71+
let need = new Array(128).fill(0);
72+
let window = new Array(128).fill(0);
73+
for (let i = 0; i < n2; ++i) {
74+
++need[t.charCodeAt(i)];
75+
}
76+
77+
let left = 0, right = 0;
78+
let res = '';
79+
let count = 0;
80+
let min = n1 + 1;
81+
while (right < n1) {
82+
let cur = s.charCodeAt(right);
83+
++window[cur];
84+
if (need[cur] > 0 && need[cur] >= window[cur]) {
85+
++count;
86+
}
87+
while (count == n2) {
88+
cur = s.charCodeAt(left);
89+
if (need[cur] > 0 && need[cur] >= window[cur]) {
90+
--count;
91+
}
92+
if (right - left + 1 < min) {
93+
min = right - left + 1;
94+
res = s.slice(left, right + 1);
95+
}
96+
--window[cur];
97+
++left;
98+
}
99+
++right;
100+
}
101+
return res;
102+
};
103+
```
104+
63105
### **...**
64106

65107
```

solution/0000-0099/0076.Minimum Window Substring/README_EN.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,46 @@
4343

4444
```
4545

46+
### **TypeScript**
47+
48+
```ts
49+
function minWindow(s: string, t: string): string {
50+
let n1 = s.length, n2 = t.length;
51+
if (n1 < n2) return '';
52+
let need = new Array(128).fill(0);
53+
let window = new Array(128).fill(0);
54+
for (let i = 0; i < n2; ++i) {
55+
++need[t.charCodeAt(i)];
56+
}
57+
58+
let left = 0, right = 0;
59+
let res = '';
60+
let count = 0;
61+
let min = n1 + 1;
62+
while (right < n1) {
63+
let cur = s.charCodeAt(right);
64+
++window[cur];
65+
if (need[cur] > 0 && need[cur] >= window[cur]) {
66+
++count;
67+
}
68+
while (count == n2) {
69+
cur = s.charCodeAt(left);
70+
if (need[cur] > 0 && need[cur] >= window[cur]) {
71+
--count;
72+
}
73+
if (right - left + 1 < min) {
74+
min = right - left + 1;
75+
res = s.slice(left, right + 1);
76+
}
77+
--window[cur];
78+
++left;
79+
}
80+
++right;
81+
}
82+
return res;
83+
};
84+
```
85+
4686
### **...**
4787

4888
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function minWindow(s: string, t: string): string {
2+
let n1 = s.length, n2 = t.length;
3+
if (n1 < n2) return '';
4+
let need = new Array(128).fill(0);
5+
let window = new Array(128).fill(0);
6+
for (let i = 0; i < n2; ++i) {
7+
++need[t.charCodeAt(i)];
8+
}
9+
10+
let left = 0, right = 0;
11+
let res = '';
12+
let count = 0;
13+
let min = n1 + 1;
14+
while (right < n1) {
15+
let cur = s.charCodeAt(right);
16+
++window[cur];
17+
if (need[cur] > 0 && need[cur] >= window[cur]) {
18+
++count;
19+
}
20+
while (count == n2) {
21+
cur = s.charCodeAt(left);
22+
if (need[cur] > 0 && need[cur] >= window[cur]) {
23+
--count;
24+
}
25+
if (right - left + 1 < min) {
26+
min = right - left + 1;
27+
res = s.slice(left, right + 1);
28+
}
29+
--window[cur];
30+
++left;
31+
}
32+
++right;
33+
}
34+
return res;
35+
};

0 commit comments

Comments
 (0)