Skip to main content

Reverse String - Solution & Explanation

EasyTwo PointersString12 min readAsked at: Amazon, Microsoft, Apple +13
Practice this problem

Problem Statement

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.

 

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:

Approach Overview

Problem Overview: You are given a character array s and must reverse it in-place. The function cannot allocate extra space for another array; the reversal must happen by modifying the original array directly.

Approach 1: Two-Pointer Approach (O(n) time, O(1) space)

This is the standard in-place solution. Use two indices: one starting at the beginning of the array (left) and one at the end (right). Swap the characters at these positions, then move left++ and right-- until the pointers meet in the middle. Each element is visited at most once, giving O(n) time complexity. Since the swaps happen directly inside the input array without extra storage, the space complexity remains O(1). This technique is a classic application of the Two Pointers pattern and works efficiently for problems where you process elements from both ends of a sequence.

The key insight is that reversing a sequence simply means exchanging symmetric elements around the center. For example, the first character swaps with the last, the second with the second-last, and so on. You only need to process half the array because each swap places two characters in their final position. This approach is optimal for array-based string manipulation problems where in-place updates are required.

Approach 2: Recursive Approach (O(n) time, O(n) space)

The recursive solution performs the same symmetric swaps but delegates the remaining work to recursive calls. Start with two indices (left and right). Swap s[left] and s[right], then recursively call the function with left + 1 and right - 1. The recursion stops when left >= right, meaning the middle of the array has been reached.

Each recursive call processes one pair of characters, so the total number of operations is still O(n). However, the recursion stack stores up to n/2 function calls, resulting in O(n) auxiliary space. While this approach is conceptually elegant and demonstrates understanding of recursion, it is less space-efficient than the iterative two-pointer method.

Recommended for interviews: The two-pointer approach is what interviewers expect. It demonstrates awareness of in-place array manipulation and optimal space usage. Mentioning the recursive version shows deeper algorithmic understanding, but the iterative two-pointer solution is the cleanest and most efficient implementation.

Approach 1: Two-Pointer Approach

The two-pointer technique involves using two pointers, one starting at the beginning of the array and the other at the end. Swap the characters at the two pointers and then move the pointers inwards towards each other until they meet or cross. This method efficiently reverses the array in-place with O(1) extra space.

The C solution uses a while loop with two pointers, left and right, to swap the characters until the entire string is reversed. The process continues until the left pointer is not less than the right pointer.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) where n is the number of characters in the string.
Space Complexity: O(1) as it uses a fixed amount of extra space.

Try this approach in the editor →

Approach 2: Recursive Approach

This approach uses recursion to swap characters at symmetric positions while visually simplifying the problem. The base case is when the left index is not less than the right index.

Recursive function in C swaps characters starting from outermost towards the middle, reducing the problem size in each call.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n).
Space Complexity: O(n) due to call stack depth.

Try this approach in the editor →

Approach 3: Two Pointers

We use two pointers i and j, initially pointing to the start and end of the array respectively. Each time, we swap the elements at i and j, then move i forward and j backward, until i and j meet.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Two-Pointer Approach

Time Complexity: O(n) where n is the number of characters in the string.
Space Complexity: O(1) as it uses a fixed amount of extra space.

Recursive Approach

Time Complexity: O(n).
Space Complexity: O(n) due to call stack depth.

Two Pointers—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Two-Pointer ApproachO(n)O(1)Best general solution when the string/array must be reversed in-place with minimal memory.
Recursive ApproachO(n)O(n)Useful for demonstrating recursion concepts or practicing divide-and-conquer thinking.

Video Solution

Reverse String - 3 Ways - Leetcode 344 - Python • NeetCode • 89,819 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Reverse String easy or hard?
Reverse String is classified as an easy problem. The logic is straightforward once you recognize the symmetric swap pattern, making it a common warm-up problem for practicing arrays, strings, and the two-pointer technique.
How to solve Reverse String in O(n)?
Use two pointers that start at opposite ends of the character array. Swap the characters at the pointers, then move them inward until they meet. This performs the reversal in linear time O(n) and constant space O(1) without allocating a new string.
Reverse String Python or Java solution?
In Python, swap characters using two indices inside a loop while left < right. In Java, use a char array and perform in-place swaps with a temporary variable. Both implementations follow the same two-pointer logic and run in O(n) time with O(1) extra space.
What is the best approach for Reverse String?
The two-pointer approach is the most efficient method. Start with one pointer at the beginning and another at the end of the array, swap the characters, and move both pointers toward the center. This completes the reversal in O(n) time and O(1) space since the operation is done in-place.
What data structure is used in Reverse String?
The problem operates directly on a character array, which behaves like a mutable string structure. The algorithm relies on index-based access and swaps, combined with the two-pointer technique for efficient traversal from both ends.
What is the time complexity of Reverse String?
The optimal solution runs in O(n) time because each character is processed at most once. In the two-pointer approach, roughly n/2 swaps occur. Recursive implementations also take O(n) time but use additional stack space.
Is Reverse String asked at Google, Amazon, or Meta?
Reverse String is a common introductory string manipulation problem that appears in coding interviews and screening rounds at companies like Amazon and Meta. It is often used to test understanding of in-place array operations and the two-pointer technique.

Ready to solve this problem?

Practice Reverse String with our built-in code editor and test cases.

Practice on FleetCode