AlgoMaster Logo

Reverse Words in a String

Last Updated: February 7, 2026

Ashish

Ashish Pratap Singh

medium

Problem Description

Solve it on LeetCode

Understanding the Problem

Before diving into solutions, let's make sure we understand exactly what we are dealing with. The problem has several nuances that will affect our implementation.

What we need to handle

  1. Multiple spaces between words: The input "a good example" has three spaces between "good" and "example", but the output should have exactly one space: "example good a".
  2. Leading spaces: The input " hello world" starts with spaces. The output should not: "world hello".
  3. Trailing spaces: Similarly, "hello world " has trailing spaces that should be removed in the output.
  4. Preserving word content: The characters within each word must remain in their original order. We only reverse the order of words, not the letters within them.
  5. Single word edge case: If the input is just "word" (possibly with surrounding spaces), the output should simply be "word".

The core operation is straightforward: identify the words, collect them, and output them in reverse order with single spaces between them. The challenge lies in doing this efficiently and handling all the spacing edge cases correctly.

Let's think about the structure of the problem. We can decompose it into steps:

  1. Parse the string to extract individual words (ignoring extra spaces)
  2. Reverse the order of these words
  3. Join them back together with single spaces

Different approaches handle these steps in different ways. Some use language built-ins, others manipulate characters directly, and some use auxiliary data structures like stacks.

Approaches

1. Using Built-in Split and Reverse

Intuition

Most programming languages provide powerful string manipulation functions. In particular, the ability to split a string by whitespace and join elements with a delimiter makes this problem almost trivial. The key insight is that split functions typically handle multiple consecutive delimiters and leading/trailing delimiters gracefully, giving us a clean list of words.

This approach is the most practical for production code. It's readable, concise, and leverages well-tested library functions. In an interview, you might mention this as your first instinct, then discuss whether the interviewer wants to see a more manual approach.

Algorithm

  1. Split the string by whitespace to get an array of words (most split functions handle multiple spaces and trim automatically)
  2. Reverse the array of words
  3. Join the words with a single space delimiter
  4. Return the result

The beauty of this approach is that it handles all the edge cases, multiple spaces, leading spaces, trailing spaces, automatically through the split operation.

Code

2. Two Pointers with Deque

Intuition:

Using a two-pointer approach along with a deque (double-ended queue) can help manage space efficiently and avoid unnecessary memory allocation beyond what is required to store words. This technique manually traverses the string and accumulates words in reverse order.

Steps:

  1. Traverse the string from end to start using two pointers to find words.
  2. Use a deque to efficiently manage the words that should appear first in the result.
  3. Append words to the front of the deque and join them at the end.

3. Two Pointers: In-place Replacement

Intuition:

This method reverses the entire string first, then reverses each word in place. This technique is more space efficient because the reversal occurs directly within the input string resorted to character arrays.

Steps:

  1. Reverse the entire character array.
  2. Reverse each word within the array.
  3. Clean up any extra spaces resulting from the reversal process.

Code: