Reverse Words in a String
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. Scan from the end with a pair of pointers marking each word's boundaries , skipping runs of spaces, and collect the words in reverse order.
Constraints
- 1 ≤ s.length ≤ 104
- s contains English letters, digits, and spaces ' '.
- There is at least one word in s.
Example
s = " hello world ""world hello"Explanation Leading, trailing, and the space between words collapse to a single separator, and the word order is reversed.
Two pointers scan from the end, collecting each word
right skips backward over trailing spaces (indices 13-14) to land on index 12 (d).
What happens in this step
s = " hello world " right starts at index 14 (the last character) s[14] = ' ', s[13] = ' ', s[12] = 'd' Step backward while the character is a space. Index 12 is the first non-space found — right stops there, at the end of the word "world".
Steps to visualize
- Point right at the last character of the string.
- Skip backward over any trailing or in-between spaces.
- Move left backward from right until it passes the start of the word.
- Collect the word between left+1 and right, then continue scanning from left.
- Join the collected words with single spaces, in the order they were found.
Walk through the code
Same walkthrough, now with the code. Press Next to move one step and watch which lines run.
right skips backward over trailing spaces (indices 13-14) to land on index 12 (d).
What happens in this step
s = " hello world " right starts at index 14 (the last character) s[14] = ' ', s[13] = ' ', s[12] = 'd' Step backward while the character is a space. Index 12 is the first non-space found — right stops there, at the end of the word "world".
Solution
function reverseWords(s) {
const words = [];
let right = s.length - 1;
while (right >= 0) {
while (right >= 0 && s[right] === ' ') {
right--;
}
if (right < 0) {
break;
}
let left = right;
while (left >= 0 && s[left] !== ' ') {
left--;
}
words.push(s.slice(left + 1, right + 1));
right = left;
}
return words.join(' ');
}- Time
- O(n)
- Space
- O(n) (for the collected words and result)
Test cases
| Input | Expected | Covers |
|---|---|---|
s = " hello world " | "world hello" | example from the docstring |
s = "hello" | "hello" | a single word with no surrounding spaces |
s = "a good example" | "example good a" | multiple spaces between words collapse to one |
s = " Bob " | "Bob" | leading and trailing spaces only, around a single word |
s = "the sky is blue" | "blue is sky the" | several plain words with single spaces between them |
s = " a " | "a" | a single-character word surrounded by spaces |