- Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathminimum_window_substring.go
45 lines (44 loc) · 942 Bytes
/
minimum_window_substring.go
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
package minimum_window_substring
funcminWindow(sstring, tstring) string {
sBytes, tBytes:= []byte(s), []byte(t)
iflen(sBytes) ==0||len(tBytes) ==0||len(sBytes) <len(tBytes) {
return""
}
sMap:=map[byte]int{}
tMap:=map[byte]int{}
for_, b:=rangetBytes {
tMap[b]++
}
begin, tmpBegin:=0, 0
minSize:=-1
letters:=0
fori:=0; i<len(sBytes); i++ {
b:=sBytes[i]
sMap[b]++
if_, ok:=tMap[b]; ok&&sMap[b] <tMap[b] {
letters++
}
ifletters==len(tBytes) {
fortmpBegin<i {
if_, ok:=tMap[sBytes[tmpBegin]]; ok {
ifsMap[sBytes[tmpBegin]] ==tMap[sBytes[tmpBegin]] {
break
}
sMap[sBytes[tmpBegin]]--
}
tmpBegin++
}
ifminSize==-1||i-tmpBegin<minSize {
minSize=i-tmpBegin
begin=tmpBegin
}
}
letters--
tmpBegin++
sMap[sBytes[tmpBegin]]--
}
ifminSize==-1 {
return""
}
returns[begin : begin+minSize+1]
}