Stamping The Sequence - Problem

Imagine you have a rubber stamp and want to create a specific word by stamping it on a blank canvas. You're given two strings:

  • stamp - your rubber stamp pattern
  • target - the final sequence you want to create

Initially, you have a string s of length target.length filled with question marks ('?'). In each turn, you can place your stamp anywhere on s and replace the characters with your stamp pattern.

Example: If stamp = "abc" and target = "abcba", you start with s = "?????". You can:

  • Place stamp at index 0: "abc??"
  • Place stamp at index 1: "?abc?"
  • Place stamp at index 2: "??abc"

Goal: Transform s into target using at most 10 * target.length turns. Return the sequence of starting positions where you placed the stamp. If impossible, return an empty array.

Key constraint: The stamp must fit completely within the string boundaries!

Input & Output

example_1.py โ€” Basic Stamping
$ Input: stamp = "abc", target = "ababc"
โ€บ Output: [0, 2]
๐Ÿ’ก Note: Start with "?????" โ†’ stamp at 0: "abc??" โ†’ stamp at 2: "ababc". The sequence [0, 2] creates the target.
example_2.py โ€” Overlapping Stamps
$ Input: stamp = "abca", target = "aabcaca"
โ€บ Output: [3, 0, 1]
๐Ÿ’ก Note: Working backwards: "aabcaca" โ†’ remove at 3: "aab????" โ†’ remove at 0: "????aca" โ†’ remove at 1: "???????" then reverse to get [1, 0, 3].
example_3.py โ€” Impossible Case
$ Input: stamp = "abc", target = "abcdef"
โ€บ Output: []
๐Ÿ’ก Note: Cannot create "abcdef" using only stamp "abc" since 'def' characters don't exist in the stamp.

Constraints

  • 1 โ‰ค stamp.length โ‰ค target.length โ‰ค 1000
  • stamp and target consist of lowercase English letters only
  • Key constraint: Must solve within 10 ร— target.length turns

Visualization

Tap to expand
Reverse Stamping StrategySTART"abcba"STEP 1"ab?ba"STEP 2"???ba"END"?????"Key Insight: Work Backwards!1. Start with target: "abcba"2. Remove stamp at pos 2: "ab?ba" โ†’ record [2]3. Remove stamp at pos 0: "???ba" โ†’ record [2,0]4. Remove stamp at pos 2: "?????" โ†’ record [2,0,2]5. Reverse result: [2,0,2] โœจ๐ŸŽฏ Forward Process: ? โ†’ ??abc โ†’ abcbc โ†’ abcbaโœ… Greedy Choice Always Works in Reverse Direction!
Understanding the Visualization
1
Start with Final Design
Begin with your target string - this is your completed artwork
2
Find Removable Stamps
Look for positions where you can 'remove' a stamp (convert matching characters to '?')
3
Greedy Removal
Always pick the first valid position to remove - greediness works here!
4
Track the Process
Record each removal position as you work backwards to a blank canvas
5
Reverse for Answer
Flip your removal sequence to get the forward stamping order
Key Takeaway
๐ŸŽฏ Key Insight: The magic happens when we reverse our thinking - instead of building up the target, we tear it down greedily, which guarantees an optimal solution!
Asked in
Google 25 Amazon 18 Microsoft 12 Meta 8
21.8K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen