Introduction

The Reverse Words in a String problem requires us to reverse the order of words in a sentence while keeping the characters inside each word unchanged.

For example, the sentence:

"I love programming"

becomes:

"programming love I"

This is different from reversing the entire string character by character.

Problem Statement

Given a string containing words separated by spaces, reverse the order of the words.

The characters inside each word should remain in their original order.

Example

Input:
"I love programming"
Output:

"programming love I"

Constraints

  • 1 ≤ n ≤ 10⁵
  • Words are separated by spaces.
  • Extra spaces may be present.
  • The order of characters within each word must remain unchanged.

Approach 1: Brute Force

Explanation

The simplest approach is to:

  1. Split the string into individual words.
  2. Store the words in an array.
  3. Traverse the array from the last word to the first.
  4. Build the reversed sentence.

Steps

  1. Read the input string.
  2. Split the string into words.
  3. Start from the last word.
  4. Add each word to the result.
  5. Return the result.

Dry Run

Input:"I love programming"

Split:

["I", "love", "programming"]
Traverse from right to left:

programming
love
I
Result:

"programming love I"

Brute Force Code

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Approach 2: Optimized Solution Using Two Pointers

Explanation

We can process the string from the end and identify each word using two pointers.

The idea is:

  • Start from the end of the string.
  • Skip spaces.
  • Find the beginning of the current word.
  • Add that word to the result.
  • Continue moving toward the beginning.

This avoids explicitly storing all words in a separate array.

Steps

  1. Start from the last character.
  2. Skip any spaces.
  3. Find the beginning of the current word.
  4. Add the word to the result.
  5. Move to the previous word.
  6. Repeat until the beginning of the string.

Dry Run

Input:"I love programming"

Start from the end:
"I love programming"
↑
Find:
programming
Result:
"programming"
Move backward:

"I love programming"
↑
Find:
love
Result:
"programming love"
Move backward:
"I love programming"
↑
Find:
I
Final Result:
"programming love I"

Optimized Code

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n) for the output string.

Edge Cases

  • Empty string → ""
  • Single word → remains unchanged.
  • Leading spaces → should be ignored.
  • Trailing spaces → should be ignored.
  • Multiple spaces between words → should produce a single space between words.
  • String containing only spaces → result is empty.

Why This Problem is Important

This problem helps develop skills in:

  • String traversal
  • Two-pointer technique
  • Word extraction
  • Space handling
  • String manipulation

It is also a common interview problem.

Real-World Applications

Reversing word order can be useful in:

  • Text processing
  • Search engines
  • Natural language processing
  • Text formatting
  • Sentence manipulation

Common Mistakes

  1. Reversing all characters instead of reversing the words.
  2. Keeping unnecessary spaces.
  3. Forgetting leading or trailing spaces.
  4. Producing multiple spaces between words.
  5. Changing the order of characters inside individual words.

Interview Tips

  • Clearly distinguish reversing words from reversing characters.
  • Handle extra spaces carefully.
  • Explain the brute-force split-and-reverse approach first.
  • Then explain how scanning from the end can process words directly.
  • Always test with leading, trailing, and multiple spaces.

Related Questions

  • Reverse a String
  • Reverse Vowels
  • Reverse String II
  • Length of Last Word
  • Longest Common Prefix
  • String Compression

Final Takeaway

To reverse the order of words in a string, process the words from right to left while keeping each word's characters unchanged.

The problem can be solved in O(n) time, making it efficient even for large strings.