Reverse a String Visualization & Animation

Swaps characters from both ends toward the middle using two pointers; O(n) time, O(1) space.

## What is it? Reversing a string means producing a new string (or modifying in-place) where characters appear in the opposite order. It is one of the most fundamental string manipulation techniques. ## How it works **Two-pointer approach (in-place for char arrays):** - Place `left = 0`, `right = n-1` - Swap `str[left]` and `str[right]` - Move `left++` and `right--` - Repeat until `left >= right` **Using a stack:** - Push each character onto a stack - Pop characters off the stack to form the reversed string ## When to use - Palindrome checking (compare original vs reversed) - Reversing words in a sentence - Part of larger string manipulation problems - Anagram detection via sorted strings ## Key Points - In-place two-pointer swap is the most space-efficient approach: O(1) - Python: `s[::-1]` reverses in O(n) time but O(n) space (new string) - Java strings are immutable — use a `char[]` or `StringBuilder`

Category: algorithms

Difficulty: beginner

Time Complexity: O(n)

Space Complexity: O(1)

Reverse a String

beginner

Swaps characters from both ends toward the middle using two pointers; O(n) time, O(1) space.

a
l
g
o
r
i
t
h
m
L / R pointers
Finalised chars
Unprocessed
Starting reverse: left=0, right=8. Input: "algorithm"