Reverse Words in a String

MediumStrings
Next
StringTwo Pointers

Reverse the order of the words in a line. Words are separated by one or more spaces, and the line may have extra spaces at the start or end. The output must have the words separated by exactly one space and no leading or trailing spaces.

Input

A single line of text with at least one word.

Output

The words in reverse order, separated by single spaces.

Constraints

1 <= length <= 10^4

Example 1
Input
the sky is blue
Output
blue is sky the

The words come out in the opposite order.

Example 2
Input
  hello   world  
Output
world hello

Extra spaces collapse to a single space between words.

Hints

Reverse Words in a String - Medium Strings Problem | Skill Shiksha