Skip to content

Commit c7a8d82

Browse files
committedMar 18, 2022
feat: add solutions to lc problem: No.0076
No.0076.Minimum Window Substring
1 parent bda8809 commit c7a8d82

File tree

4 files changed

+187
-2
lines changed

4 files changed

+187
-2
lines changed
 

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

+66-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,29 @@
6666
<!-- 这里可写当前语言的特殊实现逻辑 -->
6767

6868
```python
69-
69+
class Solution:
70+
def minWindow(self, s: str, t: str) -> str:
71+
ans = ''
72+
m, n = len(s), len(t)
73+
if m < n:
74+
return ans
75+
need = Counter(t)
76+
window = Counter()
77+
i, cnt, mi = 0, 0, float('inf')
78+
for j, c in enumerate(s):
79+
window[c] += 1
80+
if need[c] >= window[c]:
81+
cnt += 1
82+
while cnt == n:
83+
if j - i + 1 < mi:
84+
mi = j - i + 1
85+
ans = s[i: j + 1]
86+
c = s[i]
87+
if need[c] >= window[c]:
88+
cnt -= 1
89+
window[c] -= 1
90+
i += 1
91+
return ans
7092
```
7193

7294
### **Java**
@@ -202,4 +224,47 @@ public:
202224
};
203225
```
204226

227+
### **Go**
228+
229+
```go
230+
func minWindow(s string, t string) string {
231+
ans := ""
232+
m, n := len(s), len(t)
233+
if m < n {
234+
return ans
235+
}
236+
need := make([]int, 128)
237+
for _, c := range t {
238+
need[c] += 1
239+
}
240+
window := make([]int, 128)
241+
i, cnt, mi := 0, 0, m+1
242+
for j, c := range s {
243+
window[c]++
244+
if need[c] >= window[c] {
245+
cnt++
246+
}
247+
for cnt == n {
248+
if j-i+1 < mi {
249+
mi = j - i + 1
250+
ans = s[i : j+1]
251+
}
252+
c = rune(s[i])
253+
if need[c] >= window[c] {
254+
cnt--
255+
}
256+
window[c]--
257+
i++
258+
}
259+
}
260+
return ans
261+
}
262+
```
263+
264+
### **...**
265+
266+
```
267+
268+
```
269+
205270
<!-- tabs:end -->

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

+66-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,29 @@ Since the largest window of s only has one &#39;a&#39;, return empty string.
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def minWindow(self, s: str, t: str) -> str:
61+
ans = ''
62+
m, n = len(s), len(t)
63+
if m < n:
64+
return ans
65+
need = Counter(t)
66+
window = Counter()
67+
i, cnt, mi = 0, 0, float('inf')
68+
for j, c in enumerate(s):
69+
window[c] += 1
70+
if need[c] >= window[c]:
71+
cnt += 1
72+
while cnt == n:
73+
if j - i + 1 < mi:
74+
mi = j - i + 1
75+
ans = s[i: j + 1]
76+
c = s[i]
77+
if need[c] >= window[c]:
78+
cnt -= 1
79+
window[c] -= 1
80+
i += 1
81+
return ans
6082
```
6183

6284
### **Java**
@@ -190,4 +212,47 @@ public:
190212
};
191213
```
192214

215+
### **Go**
216+
217+
```go
218+
func minWindow(s string, t string) string {
219+
ans := ""
220+
m, n := len(s), len(t)
221+
if m < n {
222+
return ans
223+
}
224+
need := make([]int, 128)
225+
for _, c := range t {
226+
need[c] += 1
227+
}
228+
window := make([]int, 128)
229+
i, cnt, mi := 0, 0, m+1
230+
for j, c := range s {
231+
window[c]++
232+
if need[c] >= window[c] {
233+
cnt++
234+
}
235+
for cnt == n {
236+
if j-i+1 < mi {
237+
mi = j - i + 1
238+
ans = s[i : j+1]
239+
}
240+
c = rune(s[i])
241+
if need[c] >= window[c] {
242+
cnt--
243+
}
244+
window[c]--
245+
i++
246+
}
247+
}
248+
return ans
249+
}
250+
```
251+
252+
### **...**
253+
254+
```
255+
256+
```
257+
193258
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
func minWindow(s string, t string) string {
2+
ans := ""
3+
m, n := len(s), len(t)
4+
if m < n {
5+
return ans
6+
}
7+
need := make([]int, 128)
8+
for _, c := range t {
9+
need[c] += 1
10+
}
11+
window := make([]int, 128)
12+
i, cnt, mi := 0, 0, m+1
13+
for j, c := range s {
14+
window[c]++
15+
if need[c] >= window[c] {
16+
cnt++
17+
}
18+
for cnt == n {
19+
if j-i+1 < mi {
20+
mi = j - i + 1
21+
ans = s[i : j+1]
22+
}
23+
c = rune(s[i])
24+
if need[c] >= window[c] {
25+
cnt--
26+
}
27+
window[c]--
28+
i++
29+
}
30+
}
31+
return ans
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def minWindow(self, s: str, t: str) -> str:
3+
ans = ''
4+
m, n = len(s), len(t)
5+
if m < n:
6+
return ans
7+
need = Counter(t)
8+
window = Counter()
9+
i, cnt, mi = 0, 0, float('inf')
10+
for j, c in enumerate(s):
11+
window[c] += 1
12+
if need[c] >= window[c]:
13+
cnt += 1
14+
while cnt == n:
15+
if j - i + 1 < mi:
16+
mi = j - i + 1
17+
ans = s[i: j + 1]
18+
c = s[i]
19+
if need[c] >= window[c]:
20+
cnt -= 1
21+
window[c] -= 1
22+
i += 1
23+
return ans

0 commit comments

Comments
 (0)
Please sign in to comment.