Skip to content

Commit ed9b9d7

Browse files
committed
minimum_window_substring
1 parent d5d1c4c commit ed9b9d7

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
6565
#### [73. set matrix zeroes](https://github.com/hitzzc/go-leetcode/tree/master/set_matrix_zeroes)
6666
#### [74. search a 2D matrix](https://github.com/hitzzc/go-leetcode/tree/master/search_a_2D_matrix)
6767
#### [75. sort colors](https://github.com/hitzzc/go-leetcode/tree/master/sort_colors)
68+
#### [76. minimum window substring](https://github.com/hitzzc/go-leetcode/tree/master/minimum_window_substring)
6869

6970

7071

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package minimum_window_substring
2+
3+
func minWindow(s string, t string) string {
4+
sBytes, tBytes := []byte(s), []byte(t)
5+
if len(sBytes) == 0 || len(tBytes) == 0 || len(sBytes) < len(tBytes) {
6+
return ""
7+
}
8+
sMap := map[byte]int{}
9+
tMap := map[byte]int{}
10+
for _, b := range tBytes {
11+
tMap[b]++
12+
}
13+
begin, tmpBegin := 0, 0
14+
minSize := -1
15+
letters := 0
16+
for i := 0; i < len(sBytes); i++ {
17+
b := sBytes[i]
18+
sMap[b]++
19+
if _, ok := tMap[b]; ok && sMap[b] < tMap[b] {
20+
letters++
21+
}
22+
if letters == len(tBytes) {
23+
for tmpBegin < i {
24+
if _, ok := tMap[sBytes[tmpBegin]]; ok {
25+
if sMap[sBytes[tmpBegin]] == tMap[sBytes[tmpBegin]] {
26+
break
27+
}
28+
sMap[sBytes[tmpBegin]]--
29+
}
30+
tmpBegin++
31+
}
32+
if minSize == -1 || i-tmpBegin < minSize {
33+
minSize = i - tmpBegin
34+
begin = tmpBegin
35+
}
36+
}
37+
letters--
38+
tmpBegin++
39+
sMap[sBytes[tmpBegin]]--
40+
}
41+
if minSize == -1 {
42+
return ""
43+
}
44+
return s[begin : begin+minSize+1]
45+
}

0 commit comments

Comments
 (0)