Show problems as
Classic problem #344EasyTwo PointersString

Reverse String

Real-world version: Flip the Stamp Strip
Tracked in this browser only
Read Reverse String on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

Learn the pattern firstString Manipulation · Strings, animated step by step

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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