Last Updated: February 7, 2026
Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
Input: s = "the sky is blue"
Output: "blue is sky the"
Input: s = " hello world "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Input: s = "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.
1 <= s.length <= 104s contains English letters (upper-case and lower-case), digits, and spaces ' '.s.Follow-up: If the string data type is mutable in your language, can you solve it in-place with O(1) extra space?
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.
"a good example" has three spaces between "good" and "example", but the output should have exactly one space: "example good a"." hello world" starts with spaces. The output should not: "world hello"."hello world " has trailing spaces that should be removed in the output."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:
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.
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.
The beauty of this approach is that it handles all the edge cases, multiple spaces, leading spaces, trailing spaces, automatically through the split operation.
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.
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.