This looks like a "split and reverse" problem, and in many languages it is close to that. Two details make it harder than it appears.
First, the input can have leading spaces, trailing spaces, and multiple spaces between words. The output must have exactly one space between words and no extra spaces at either end. Reversing the entire string is not enough, because the spaces would land in the wrong places.
Second, the follow-up asks whether you can solve it in O(1) extra space when the string is mutable. The in-place idea is to reverse the entire string first, which puts the words in the correct order but spells each word backwards, then reverse each word individually to restore it. The sections below build up to this.
1 <= s.length <= 10^4 → The string is small enough that an O(n) scan is fast, and large enough that we should avoid repeatedly building intermediate strings inside a loop, which can degrade to O(n^2).s contains letters, digits, and spaces → A word is any contiguous run of non-space characters. There are no other delimiters to handle.Use the language's built-in string operations. Split the string on spaces to get the individual words, drop any empty strings produced by consecutive spaces, reverse the list of words, and join them back with a single space.
This is a valid solution. The follow-up is usually to do it without split and join, which Approach 2 covers.
s by spaces to get an array of tokensThis approach relies on built-in split and join. The next approach extracts words by scanning the string directly, without any split helper.
Scan the string from right to left and extract words by index. Traversing backwards visits the words in reverse order, which is the order we want them in the output, so no separate reversal step is needed.
Two pointers mark the boundaries of each word. Start at the end of the string, skip trailing spaces, then move left until the start of the word. The slice between those positions is one word. Append it to the result and repeat until the whole string is processed.
i to the last index of the stringi >= 0:i while s[i] is a spacei < 0, break (we've processed everything)end = i (this is the last character of the current word)i left while s[i] is not a space and i >= 0 to find the start of the words[i+1 ... end]Take s = " hello world " (indices 0 to 14, with two leading and two trailing spaces). The pointer i starts at 14.
Both approaches so far use O(n) extra space for the result. The next approach rearranges characters within the input itself, reversing the entire string first and then reversing each word.
This answers the follow-up: "Can you do it in O(1) extra space if the string is mutable?"
Consider the string "the sky is blue":
"eulb si yks eht""blue is sky the"Two passes reverse the word order while keeping each word intact. Why this works: reversing the whole string maps the word at positions [a, b] to positions [n-1-b, n-1-a], which preserves the relative order of words back-to-front, and reversing a contiguous block twice (once as part of the whole, once on its own) returns it to its original spelling.
Extra spaces still need handling. After the full reversal, runs of spaces remain, and the second pass uses a write pointer to compact them so exactly one space separates words and no spaces remain at the ends.
Take s = "a good example" (16 characters, with three spaces between "good" and "example"). The expected answer is "example good a".