Reverse Words in a String
Maintained by Vikas D, software engineer
You are handed a string that holds some words. A word is any run of characters that are not spaces, and the words are separated by one or more spaces.
Your job is to flip the order of the words so the last word comes first and the first word comes last. The characters inside each word stay as they are; only the word order changes.
The output must be tidy: no leading or trailing spaces, and exactly one space between consecutive words even if the input padded them with several. If the input is a single word, you return that word unchanged.
Examples
Four words in order the, sky, is, blue become blue, is, sky, the when reversed.
The padding on both ends is dropped, leaving hello and world, which reverse to world hello.
The triple space between good and example collapses to one, and the three words reverse to example good a.
Constraints
- 1 <= s.length <= 10^4
- s contains English letters, digits, and spaces only
- There is at least one word, so the result is never empty
- Words are separated by one or more spaces, and the input may begin or end with spaces
Visible test cases
- reverseWords("the sky is blue") == "blue is sky the"
- reverseWords(" hello world ") == "world hello"
- reverseWords("a good example") == "example good a"
- reverseWords("singleword") == "singleword"
- reverseWords(" spaces everywhere ") == "everywhere spaces"
Hints
Hint 1
First isolate the actual words by skipping over any blocks of spaces. The spacing in the input does not matter once you have the list of words.
Hint 2
Once you have the words in original order, reversing that list and joining with a single space gives the answer directly.
Hint 3
Watch the edge cases: leading spaces, trailing spaces, and multiple spaces between words must all produce clean single-space output.
Approaches
Split, reverse, join
Time O(n) · Space O(n)Let the language tokenize on whitespace, which naturally discards empty pieces from extra spaces, then reverse the token list and rejoin.
- Split the string on runs of whitespace so only non-empty words remain.
- Reverse the resulting list of words.
- Join the reversed list with a single space and return it.
Single manual scan
Time O(n) · Space O(n)Walk the string once with an index, skipping spaces and slicing out each word yourself, so you control the tokenizing without relying on a built-in splitter.
- Set an index at the start and loop while it is inside the string.
- Advance past any spaces, then if you are past the end, stop.
- Mark the word start, advance until the next space or the end, and slice that word into a list.
- After collecting all words, reverse the list and join with a single space.
Common mistakes
- Forgetting to collapse multiple internal spaces, which leaves double spaces in the output.
- Leaving a leading or trailing space because the input was padded and you reversed the raw characters instead of the words.
- Reversing the characters of each word instead of reversing the order of whole words.
- Splitting on a single space literal, which produces empty strings for each extra space and corrupts the join.
Follow-up variations
- Reverse the words in place using O(1) extra space when the input is a mutable character array.
- Handle other whitespace such as tabs and newlines as word separators, not just the space character.
- Reverse the words but keep the original spacing between them intact rather than normalizing to single spaces.