AlgoMaster Logo

Reverse String

easyFrequency5 min readUpdated June 23, 2026

Understanding the Problem

We're given an array of characters and need to reverse it in place. "In place" means we can't create a new array and copy the reversed characters into it. We need to rearrange the existing array using only O(1) extra memory.

Reversing an array means the first element swaps with the last, the second swaps with the second-to-last, and so on. Each element has exactly one partner it needs to trade places with, which points to working from both ends toward the middle.

Key Constraints:

  • 1 <= s.length <= 10^5 → With n up to 100,000, the solution must run in O(n) time. Combined with the O(1) space requirement, this points to an in-place two-pointer swap.
  • O(1) extra memory → Constant space, so no reversed copy is allowed. A recursive solution uses O(n) stack space, which violates this in the strict sense.
  • s[i] is a printable ASCII character → Single-character data with no multi-byte or encoding concerns.

Approach 1: Brute Force (Extra Array)

Intuition

Create a new array, fill it with the characters in reverse order, then copy everything back into the original array. The logic stays simple: read from the end of the input and write to the beginning of the copy.

This violates the O(1) space constraint, but it sets up what "reversing" means before we remove the extra array.

Algorithm

  1. Create a new array reversed of the same size as s.
  2. Iterate through s from the end to the beginning, placing each character at the next position in reversed.
  3. Copy all characters from reversed back into s.

Example Walkthrough

Input:

0
h
1
e
2
l
3
l
4
o
s

After reading from end to front into reversed:

0
o
1
l
2
l
3
e
4
h
reversed

Finally, copy reversed back into s:

0
o
1
l
2
l
3
e
4
h
s

Code

This works but uses extra space, violating the constraint. Since each character has exactly one destination, its mirror position, we can swap each pair in the original array directly and skip the copy.

Approach 2: Two Pointers (Optimal)

Intuition

Reversing an array is a series of swaps between mirror positions. Position 0 swaps with position n-1, position 1 swaps with position n-2, and so on. This needs no extra space, only two pointers moving toward each other from opposite ends.

Algorithm

  1. Initialize left = 0 and right = s.length - 1.
  2. While left < right:
    • Swap s[left] with s[right].
    • Increment left, decrement right.
  3. When left >= right, the array is fully reversed.

Example Walkthrough

1Initialize: left=0, right=4
0
h
left
1
e
2
l
3
l
4
o
right
1/6

Code

The two-pointer approach is optimal in both time and space. The same swap-the-ends logic can also be written recursively, which does not improve performance but expresses the problem as a smaller version of itself.

Approach 3: Recursion

Intuition

Reversal is self-similar. Swap the first and last characters, and the remaining work is "reverse everything in between," which is the same problem on a smaller array. This continues until the range holds one character or none, where there is nothing to swap.

This does not improve on the two-pointer solution. It is worse, because the call stack adds O(n) space for what the iterative version does in O(1).

Algorithm

  1. Define a helper function that takes left and right indices.
  2. Base case: if left >= right, return (nothing to swap).
  3. Recursive case: swap s[left] with s[right], then recurse on left + 1 and right - 1.

Example Walkthrough

1Call 1: left=0, right=5
0
H
left
1
a
2
n
3
n
4
a
5
h
right
1/6

Code