AlgoMaster Logo

Reverse String

Last Updated: March 29, 2026

easy

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.

The key observation is simple but important: 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. This naturally suggests working from both ends toward the middle.

Key Constraints:

  • 1 <= s.length <= 10^5 → With n up to 100,000, we need O(n) or better. Combined with the O(1) space requirement, the two-pointer swap approach fits perfectly.
  • O(1) extra memory → The problem explicitly requires constant space. No creating a reversed copy. A recursive approach uses O(n) stack space, which technically violates this.
  • s[i] is a printable ASCII character → Simple character data, no special encoding considerations.

Approach 1: Brute Force (Extra Array)

Intuition

The most natural first thought: create a new array, fill it with the characters in reverse order, then copy everything back into the original array. This makes the logic dead simple since we just read from the end and write to the beginning.

Of course, this violates the O(1) space constraint. But it's a useful starting point to understand what "reversing" actually means before we optimize.

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 approach works correctly but uses extra space, violating the problem's constraint. Since each character has exactly one destination (its mirror position), what if we just swapped each pair directly?

Approach 2: Two Pointers (Optimal)

Intuition

Reversing an array is just a series of swaps between mirror positions. Position 0 swaps with position n-1, position 1 swaps with position n-2, and so on. We don't need any extra space for this, just two pointers walking 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 already optimal in both time and space. But can we express the same logic recursively? This won't improve performance but demonstrates a different way of thinking.

Approach 3: Recursion

Intuition

The recursive approach sees reversal as a self-similar problem. If you swap the first and last characters, the subproblem is "reverse everything in between." That subproblem has the same structure, just on a smaller array. This continues until the subproblem is a single character (or empty), at which point there's nothing to reverse.

This approach doesn't improve on the two-pointer solution. It's actually worse because of the O(n) call stack overhead. But interviewers sometimes ask for it to test your comfort with recursion.

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