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:
- Split the string into individual words.
- Store the words in an array.
- Traverse the array from the last word to the first.
- Build the reversed sentence.
Steps
- Read the input string.
- Split the string into words.
- Start from the last word.
- Add each word to the result.
- 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
- Start from the last character.
- Skip any spaces.
- Find the beginning of the current word.
- Add the word to the result.
- Move to the previous word.
- 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
- Reversing all characters instead of reversing the words.
- Keeping unnecessary spaces.
- Forgetting leading or trailing spaces.
- Producing multiple spaces between words.
- 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.