Skip to main content
Back to problems
#344
Easy Algorithms

Reverse string

Two Pointers String
80.6% acceptance
Jan 12, 2026
9509
1215
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn reverse_string(s: &mut Vec<char>) {
    let mut left = 0;
    let mut right = s.len() - 1;
    
    while left < right {
      s.swap(left, right);
      left += 1;
      right -= 1;
    }
  }
}