Skip to main content
Back to problems
#557
Easy Algorithms

Reverse words in a string iii

Two Pointers String
83.9% acceptance
Jan 13, 2026
6203
256
Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn reverse_words(s: String) -> String {
    s.split(' ')
      .map(|w| w.chars().rev().collect::<String>())
      .collect::<Vec<_>>()
      .join(" ")
  }
}