◀ THE GRIND — TWO POINTERS

Reverse String

The drill: Flip a character array end-to-end by mutating the buffer you were handed — no second array. Encoded here as an array of one-character strings, exactly how LeetCode's Python signature types it.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

A buffer of single characters arrives, and the task is to flip it end-to-end so the last character becomes first and the first becomes last, with nothing returned separately.

The array itself is the answer: every character keeps its identity, only positions change, and the reversal has to happen inside the same buffer rather than producing a fresh one.

Whitespace and repeated characters are ordinary members of the array with no special handling; an array of length one or zero simply reverses to itself.

EX 01
s = ["g", "r", "i", "n", "d"]
["d", "n", "i", "r", "g"]
EX 02
s = ["a"]
["a"]
SINGLE ELEMENT — NOTHING TO SWAP
EX 03
s = ["x", "y"]
["y", "x"]
SMALLEST REAL SWAP
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

Building the reversed copy and pasting it back is correct — and it allocates precisely the buffer the follow-up dares you to live without.

HINT 2 THE STRUCTURE

Reversal is nothing but independent swaps: position i trades places with position n−1−i. No intermediate storage is ever required for that trade.

HINT 3 ONE STEP FROM THE ANSWER

Two pointers, one at each end: swap what they hold, step both inward, stop when they meet. The middle element of an odd-length array never has to move.

COACH'S BOARD — THE PATTERN, STEP BY STEP
SWAP FROM BOTH ENDSPATTERN · TWO POINTERSs = ["g", "r", "i", "n", "d"]
g
r
i
n
d
STEP 1

Two pointers, L at 0 and R at 4 — swap what they hold and step inward.

STEP 1 / 6 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/reverse-string.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def reverseString(self, s: List[str]) -> None:
        l, r = 0, len(s) - 1
        while l < r:
            s[l], s[r] = s[r], s[l]
            l += 1
            r -= 1
TIME O(N)SPACE O(1)PYTHON · RACE PACE · 7 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED