-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimum_windows_substring.go
More file actions
59 lines (39 loc) · 1.07 KB
/
Copy pathminimum_windows_substring.go
File metadata and controls
59 lines (39 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import "fmt"
func minWindow(s string, t string) string {
scount := make(map[int]int,0)
for _,char := range s {
scount[int(char)] = 0
}
for _,char := range t {
scount[int(char)]+=1
}
start,end,minStart,minLen,numOfCharacters := 0,0,math.MaxInt32,math.MaxInt32,len(t)
for end < len(s) {
char := int(s[end])
if val,ok := scount[char] ; ok && val > 0 {
numOfCharacters -=1
}
scount[char] -=1
for numOfCharacters == 0 {
if minLen > end-start+1 {
minStart = start
minLen = end-start+1
}
char = int(s[start])
if val,ok := scount[char] ; ok && val >= 0 {
numOfCharacters+=1
}
scount[char] += 1
start+=1
}
end+=1
}
if minLen == math.MaxInt32 {
return ""
} else {
return s[minStart:minStart+minLen]
}
}
func main() {
}