Reverse the order of the words
s holds words separated by spaces. A word is a run of characters that are not spaces. There may be extra spaces between words and at either end.
Return the words in reverse order, joined by single spaces, with no space at either end. The words themselves are not changed.
- Input
s = "interview prep starts today"- Output
"today starts prep interview"
Four words, last first.
- Input
s = " hello "- Output
"hello"
The spaces at both ends go.
- Input
s = "a b c"- Output
"c b a"
Two spaces between words become one.
Limits
1 <= s.length <= 100000s holds printable ASCII characterss holds at least one word
Hints
Hint 1
Split the string into words, skipping runs of spaces, then join them from the last to the first. In a language with mutable strings you can also reverse the whole string and then reverse each word back.
Where this was reported
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.
