Reverse String

Easy Top 250
Associated Patterns
Interviewed At (Company Tags)
MicrosoftAppleAmazonGoogle

Problem Description

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.


Examples

Example 1:

Input: s = [“h”,“e”,“l”,“l”,“o”] Output: [“o”,“l”,“l”,“e”,“h”]

Example 2:

Input: s = [“H”,“a”,“n”,“n”,“a”,“h”] Output: [“h”,“a”,“n”,“n”,“a”,“H”]


Constraints

  • 1 <= s.length <= 10⁵
  • s[i] is a printable ascii character.

Swapping Symmetric Elements

To reverse an array of characters, you can place a pointer at the beginning (left) and another at the end (right). Swap the characters at these two pointers, then increment left and decrement right. Repeat this process until the pointers meet or cross in the middle. This reverses the entire array in a single linear pass with zero extra memory allocation.


Solution 1: Left and Right Pointers Swap

In-place character swaps using two moving pointers.

class Solution {
    public void reverseString(char[] s) {
        int left = 0;
        int right = s.length - 1;
        while (left < right) {
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
    }
}
class Solution:
    def reverseString(self, s: list[str]) -> None:
        left, right = 0, len(s) - 1
        while left < right:
            s[left], s[right] = s[right], s[left]
            left += 1
            right -= 1
    # Note: Python also supports s.reverse() but this showcases the manual algorithm
#include <vector>
#include <utility>

class Solution {
public:
    void reverseString(std::vector<char>& s) {
        int left = 0;
        int right = s.size() - 1;
        while (left < right) {
            std::swap(s[left], s[right]);
            left++;
            right--;
        }
    }
};

Complexity Analysis

  • Time Complexity: O(n) as we visit and swap half of the characters in the array.
  • Space Complexity: O(1) auxiliary space as we only use primitive index pointers.

Where It Breaks

If the characters represent multi-byte encodings (like UTF-8 code units) instead of individual ASCII characters, swapping them bytes-by-bytes can break character sequences. However, since the input is defined as a clean array of printable ASCII characters, this is not an issue here.


Common Mistakes

  • Incorrect loop boundary: Loop condition written as left <= right which does a redundant swap on the middle element.
  • Off-by-one errors: Initializing the right pointer to s.length instead of s.length - 1.

Frequently Asked Questions

Is there a recursive solution? Yes, you can swap the ends and call the function recursively on the inner slice. However, a recursive approach uses O(n) space on the call stack, which is less optimal than the O(1) space iterative approach.

Does this modify the original string? Yes, it modifies the characters array in-place, which directly reflects the change on the caller’s side.


← All Problems