Stamping the Sequence
This is a draft post.
February 2021
You want to form a At the beginning, your sequence is 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 For example, if the initial sequence is “?????", and your stamp is 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. For example, if the sequence is “ababc”, and the stamp is Also, if the sequence is possible to stamp, it is guaranteed it is possible to stamp within Example 1: Example 2: Note: Accepted
10.9K
Submissions
23KProblem #926, LeetCode
target
string of lowercase letters.target.length
'?'
marks. You also have a stamp
of lowercase letters.10 * target.length
turns."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.)"abc"
, then we could return the answer [0, 2]
, corresponding to the moves “?????” -> “abc??” -> “ababc”.10 * target.length
moves. Any answers specifying more than this number of moves will not be accepted.Input: stamp = "abc", target = "ababc"
Output: [0,2]
([1,0,2] would also be accepted as an answer, as well as some other answers.)
Input: stamp = "abca", target = "aabcaca"
Output: [3,0,1]
1 <= stamp.length <= target.length <= 1000
stamp and target only contain lowercase letters.
Skipping the worst case scenario trying all possible combination within the 10 stampings, I think I would start with finding the full stamp sequence first. With the example 1, take Then we have This quickly becomes complex as the Todo:Solution
??abc
and put the position 2
into a first in last out stack (doesn’t really matter, just keep track of the order).ab???
, which we’ll do a substring match with the stamp. We put the position 0
into the stack, and output the result: [0, 2]
target.length
increases. The problem limits it to 1000, and stamp.length
is always smaller or equal to the target.length
. Having 10 stamp limit also reduces the complexity.
Can some stamp configuration produce no solution, whilst globally a solution exists?
Tags: coding challenges draft