Problem
Write a function that takes a string as input and returns the string reversed.
You must do this by modifying the input array in-place with O(1)
extra memory.
Examples
Example 1
|
|
Example 2
|
|
Similar Problems
Solution
Below are multiple methods, ordered from the most standard (two pointers) to less typical (XOR swap, library helpers). All demonstrate the same logical reversal with different trade-offs.
Method 1 - Two Pointers (Canonical In-Place)
Intuition
Use one pointer at the left end and one at the right end; swap and converge. This minimizes space and is the standard interview solution.
Approach
Initialize l=0
, r=n-1
. While l < r
, swap characters, increment l
, decrement r
.
Code
|
|
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
– each index swapped at most once. - 🧺 Space complexity:
O(1)
– in-place swaps only.
Method 2 - Symmetric Index Swap (Half Loop)
Intuition
Equivalent to Method 1 but frames the loop over the first half only; directly swaps i
with n-i-1
.
Approach
For i
in [0 .. n/2)
, swap s[i]
and s[n-1-i]
.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
– half the iterations, but still linear. - 🧺 Space complexity:
O(1)
– constant extra space.
Method 3 - Recursion
Intuition
Recurse inward: swap the outer pair then recurse on the substring interior. Pedagogical but less efficient due to call overhead.
Approach
Define helper recur(s, l, r)
: if l >= r
stop; swap s[l], s[r]
; call recur(l+1, r-1)
.
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
– each position processed once. - 🧺 Space complexity:
O(n)
– recursion stack depth≈ n/2
→O(n)
call frames.
Method 4 - Stack (Uses Extra Space)
Intuition
Push all characters, then pop to rebuild reversed order. Demonstrates LIFO data structure but wastes memory.
Approach
Traverse string pushing chars; pop sequentially and overwrite original array.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
– one pass push + one pass pop. - 🧺 Space complexity:
O(n)
– stack stores all characters.
Method 5 - In-Place XOR Swap (Not Recommended)
Intuition
Swaps two characters without a temporary variable using XOR: a ^= b; b ^= a; a ^= b;
. Conceptually clever but modern compilers optimize a normal temp swap equally (or better). Less readable and can be error-prone if indices alias.
Approach
Same two-pointer convergence as Method 1, but uses XOR sequence instead of a temporary variable for each swap.
Code
|
|
Trade-offs
Readable temp-swap (Method 1) is preferred in production; XOR adds zero practical benefit and can inhibit some optimizations / confuse reviewers.
Complexity
- ⏰ Time complexity:
O(n)
– identical iteration pattern to Method 1. - 🧺 Space complexity:
O(1)
– in-place.
Method 6 - Language Built-ins / Convenience
Intuition
Leverage library provisions: builder reversal or slicing constructs that internally perform efficient reversal (often similar to Method 1 under the hood).
Approach
Use high-level APIs to emphasize intent over mechanics.
Code
|
|
|
|
Complexity
- ⏰ Time complexity:
O(n)
– must copy / iterate through all characters. - 🧺 Space complexity:
O(n)
– new reversed buffer created.
Method Comparison
Method | Time | Space | Notes |
---|---|---|---|
1 Two Pointers | O(n) |
O(1) |
Canonical, optimal |
2 Symmetric Half Loop | O(n) |
O(1) |
Equivalent to Method 1 style variant |
3 Recursion | O(n) |
O(n) |
Educational; stack overhead |
4 Stack | O(n) |
O(n) |
Demonstrates stack; inefficient |
5 XOR Swap | O(n) |
O(1) |
No real benefit; readability cost |
6 Built-ins | O(n) |
O(n) |
Most concise; allocates new buffer |
Notes
Preferred practical answer: Method 1 (or Method 2 variant). Include XOR only to illustrate bitwise swap patterns (see also Swap two number in place without temporary variables). Recursion and stack approaches are mainly pedagogical.