[Medium] 151. Reverse Words in a String
Given an input string s, reverse the order of the words. A word is a sequence of non-space characters. Words are separated by at least one space. Return a string with words in reverse order, joined by a single space (no leading/trailing spaces, no extra spaces between words).
Examples
Example 1:
Input: s = "the sky is blue"
Output: "blue is sky the"
Example 2:
Input: s = " hello world "
Output: "world hello"
Example 3:
Input: s = "a good example"
Output: "example good a"
Constraints
1 <= s.length <= 10^4scontains English letters, digits, and spaces' '- There is at least one word in
s
Thinking Process
Key Challenges
- Leading/trailing spaces – must be stripped
- Multiple spaces between words – must be collapsed to one
- Reverse word order – not character order
Approach 1: Deque (Collect Words in Reverse)
Trim leading/trailing spaces, scan left to right building words, and push each completed word to the front of a deque. This naturally reverses the order.
Approach 2: Reverse Entire String + Reverse Each Word (In-Place)
For an O(1) extra space solution:
- Reverse the entire string
- Reverse each individual word
- Clean up extra spaces
Common Approaches
Typical techniques for this pattern:
| Approach | Time | Space | Notes |
|---|---|---|---|
| Opposite ends (this problem) | O(n) | O(1) | Sorted array pair search, reversal |
| Slow / fast pointers | O(n) | O(1) | Linked list middle, cycle detection |
| Same-direction chase | O(n) | O(1) | Remove duplicates in-place |
| Sliding window (variable) | O(n) | O(1) | Subarray with constraint |
Solution
Input: s = "the sky is blue"
Output: "blue is sky the"
Solution Explanation
Approach: Opposite ends (this problem)
Key idea: ### Key Challenges
How the code works:
- Leading/trailing spaces – must be stripped
- Multiple spaces between words – must be collapsed to one
- Reverse word order – not character order
- Reverse the entire string
- Reverse each individual word
- Clean up extra spaces
Walkthrough — input s = "the sky is blue", expected output "blue is sky the":
- Initialize variables from the problem setup.
- Apply the main loop / recursion until the condition is met.
- Confirm the result matches the expected output.
Comparison
| Approach | Time | Extra Space | Notes |
|---|---|---|---|
| Deque | O(n) | O(n) | Clean, easy to understand |
| Reverse Twice | O(n) | O(1) | In-place, interview follow-up |
Common Mistakes
- Not handling multiple consecutive spaces (outputting extra spaces between words)
- Forgetting the last word (no trailing space to trigger word completion)
- In the in-place approach: not compacting spaces during the write pass
Key Takeaways
- “Reverse word order” has two classic approaches: collect-in-reverse (deque/stack) or reverse-entire-then-reverse-each-word
- The in-place “reverse twice” technique is a common interview follow-up: “Can you do it in O(1) space?”
- Trimming and compacting spaces is the fiddly part – the deque approach sidesteps it by only collecting non-empty words
Related Problems
- 186. Reverse Words in a String II – in-place on char array
- 557. Reverse Words in a String III – reverse each word (not word order)
- 58. Length of Last Word – word parsing with trailing spaces
- 1768. Merge Strings Alternately – string traversal
References
- LC 151: Reverse Words in a String on LeetCode
- LeetCode Discuss — LC 151: Reverse Words in a String
- LeetCode Editorial (may require premium)