Reverse String
Real-world version: Flip the Stamp StripThe statement here is our own: the same inputs, outputs and technique.
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.
def flip_strip(strip: list[str]) -> list[str]:public char[] flipStrip(char[] strip)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 9 test cases: 9 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's