Easy · Token Reversal
Understand → Visualise → Practice.
Return words in reverse order with exactly one space between words and none at the ends.
Splitting on whitespace drops extra spaces automatically; reversing the word list and joining with one space gives the answer.
This removes leading, trailing and repeated spaces.
Reverse the list, not the characters.
Join the words with exactly one space.
O(n) time and O(n) space.
Splitting on a single ' ' keeps empty strings between repeated spaces.
Read the full guide first: Strings: Frequencies, Matching and Palindromes.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.