forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stamping-the-sequence.go
executable file
·62 lines (52 loc) · 1.03 KB
/
stamping-the-sequence.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
57
58
59
60
61
62
package problem0936
import "strings"
// ref: https://leetcode.com/problems/stamping-the-sequence/discuss/189254/Python-Greedy-and-DFS
func movesToStamp(stamp string, target string) []int {
s, t := []byte(stamp), []byte(target)
sSize, tSize := len(stamp), len(target)
res := make([]int, 0, 1000)
// isStamped return true if stamped since i
isStamped := func(i int) bool {
canStamp := false
for j := 0; j < sSize; j++ {
if t[i+j] == '?' {
continue
}
if t[i+j] != s[j] {
return false
}
canStamp = true
}
if canStamp {
for k := i; k < i+sSize; k++ {
t[k] = '?'
}
res = append(res, i)
}
return canStamp
}
maxIndex := tSize - sSize + 1
for {
isChanged := false
for i := 0; i < maxIndex; i++ {
isChanged = isChanged || isStamped(i)
}
if !isChanged {
// no more place to stamp
break
}
}
reverse(res)
if string(t) == strings.Repeat("?", tSize) {
return res
}
return nil
}
func reverse(a []int) {
l, r := 0, len(a)-1
for l < r {
a[l], a[r] = a[r], a[l]
l++
r--
}
}