AlgoInsight

DSA problem

Reverse String

EasyStringsPattern: Two Pointerstop600AmazonGoogleMicrosoftApple

Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory. Do not allocate a new array or use built-in reverse helpers; the goal is to demonstrate the in-place two-pointer technique.

The string consists of printable ASCII characters and may include letters, digits, and symbols. After the function returns, the array s should contain the same characters in reverse order.

Examples

Example 1
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Explanation: The characters are swapped from the outside in: h<->o and e<->l, leaving the middle l untouched.
Example 2
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
Explanation: Each pair of characters mirrored across the center is swapped; the array has even length so every character moves.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

stringtwo-pointersin-place

Browse all DSA problems