All problems
0068EasyTwo PointersString

Flip the Stamp Strip

Tracked in this browser only
Write code

Trains the technique from

LeetCode 344Reverse String

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

An engraving machine loaded a strip of stamps back to front. The strip is given as strip, a list whose entries are single-character strings, one printable ASCII character per stamp, listed from the front of the strip to the back.

Re-seat the stamps so the strip reads back to front: the stamp at the back moves to the front slot, the one before it moves to the second slot, and so on.

The machine has no spare tray, so you may not build a second list of stamps. Rearrange the entries inside strip itself, using only a constant amount of extra space on top of the input, then return that same list so the result can be inspected.

Examples

Example 1

Input
strip = ["s", "t", "a", "m", "p"]
Output
["p", "m", "a", "t", "s"]

The five stamps end up in the opposite order, and the middle one never moves.

Example 2

Input
strip = [" ", "~", "0", "Z"]
Output
["Z", "0", "~", " "]

Spaces, digits and punctuation are ordinary stamps and are re-seated like any letter.

Example 3

Input
strip = ["Q"]
Output
["Q"]

A one-stamp strip already reads the same in both directions, so nothing moves.

Constraints

  • 1 <= strip.length <= 10^5
  • strip[i] is a string of exactly one printable ASCII character
  • Only O(1) extra space is allowed; the stamps must be re-seated inside the given list

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def flip_strip(strip: list[str]) -> list[str]:
Java
public char[] flipStrip(char[] strip)
September 7
Apply