-
Notifications
You must be signed in to change notification settings - Fork 1
/
seq.go
56 lines (53 loc) · 799 Bytes
/
seq.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
46
47
48
49
50
51
52
53
54
55
56
package seq
import "sort"
func IsSub(s, t string) bool {
m, n := len(s), len(t)
if m == 0 {
return true
}
if m > n {
return false
}
idxs, idxt := 0, 0
for idxt < n {
if s[idxs] == t[idxt] {
idxs += 1
if idxs == m {
return true
}
}
idxt += 1
}
return false
}
func IsSubBS(s, t string) bool {
m, n := len(s), len(t)
if m == 0 {
return true
}
if m > n {
return false
}
dict := make(map[rune][]int)
for i, c := range t {
if dict[c] == nil {
dict[c] = make([]int, 0, n)
}
dict[c] = append(dict[c], i)
}
prev := 0
for _, c := range s {
if dict[c] == nil {
return false
}
tt := dict[c]
j := sort.Search(len(tt), func(i int) bool {
return tt[i] >= prev
})
if j == len(tt) {
return false
}
prev = tt[j] + 1
}
return true
}