Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 07477d7

Browse files
committed
0936 finish
1 parent c1536d1 commit 07477d7

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

Algorithms/0936.stamping-the-sequence/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
You want to form a target string of lowercase letters.
44

5-
At the beginning, your sequence is target.length '?' marks. You also have a stamp of lowercase letters.
5+
At the beginning, your sequence is target.length '?' marks. You also have a stamp of lowercase letters.
66

7-
On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp. You can make up to 10 * target.length turns.
7+
On each turn, you may place the stamp over the sequence, and replace every letter in the sequence with the corresponding letter from the stamp. You can make up to 10 * target.length turns.
88

9-
For example, if the initial sequence is "?????", and your stamp is "abc", then you may make "abc??", "?abc?", "??abc" in the first turn. (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.)
9+
For example, if the initial sequence is "?????", and your stamp is "abc", then you may make "abc??", "?abc?", "??abc" in the first turn. (Note that the stamp must be fully contained in the boundaries of the sequence in order to stamp.)
1010

11-
If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn. If the sequence is not possible to stamp, return an empty array.
11+
If the sequence is possible to stamp, then return an array of the index of the left-most letter being stamped at each turn. If the sequence is not possible to stamp, return an empty array.
1212

1313
For example, if the sequence is "ababc", and the stamp is "abc", then we could return the answer [0, 2], corresponding to the moves "?????" -> "abc??" -> "ababc".
1414

Algorithms/0936.stamping-the-sequence/stamping-the-sequence.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import "strings"
55
// ref: https://leetcode.com/problems/stamping-the-sequence/discuss/189254/Python-Greedy-and-DFS
66

77
func movesToStamp(stamp string, target string) []int {
8-
t, s := []byte(target), []byte(stamp)
9-
tSize, sSize := len(t), len(s)
8+
s, t := []byte(stamp), []byte(target)
9+
sSize, tSize := len(stamp), len(target)
1010

11-
res := make([]int, 0, tSize)
11+
res := make([]int, 0, 1000)
1212

13+
// isStamped return true if stamped since i
1314
isStamped := func(i int) bool {
1415
canStamp := false
1516
for j := 0; j < sSize; j++ {
@@ -30,11 +31,16 @@ func movesToStamp(stamp string, target string) []int {
3031
return canStamp
3132
}
3233

33-
changed := true
34-
for changed {
35-
changed = false
36-
for i := 0; i < tSize-sSize+1; i++ {
37-
changed = changed || isStamped(i)
34+
maxIndex := tSize - sSize + 1
35+
36+
for {
37+
isChanged := false
38+
for i := 0; i < maxIndex; i++ {
39+
isChanged = isChanged || isStamped(i)
40+
}
41+
if !isChanged {
42+
// no more place to stamp
43+
break
3844
}
3945
}
4046

Algorithms/0936.stamping-the-sequence/stamping-the-sequence_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ var tcs = []struct {
1313
target string
1414
}{
1515

16+
{
17+
"ab",
18+
"aba",
19+
},
20+
1621
{
1722
"abc",
1823
"ababc",
@@ -23,10 +28,7 @@ var tcs = []struct {
2328
"aabcaca",
2429
},
2530

26-
{
27-
"ab",
28-
"aba",
29-
},
31+
//
3032
}
3133

3234
func isCorrect(stamp, target string, sequence []int) bool {

0 commit comments

Comments
 (0)