Skip to content
Onsite

Reverse Words in a String

EasyP2Coding

Reverse the order of the words in s and return the result. A word is a maximal run of non-space characters. The result must have exactly one space between words and no leading or trailing spaces, even if s had extra spaces.

Example 1

Input
s = "the sky is blue"
Output
"blue is sky the"

Example 2

Input
s = " hello world "
Output
"world hello"

Example 3

Input
s = "a good example"
Output
"example good a"

Constraints

  • 1 <= s.length <= 10^4
  • s contains letters, digits and spaces.
  • s contains at least one word.

Hints

Run checks the 3 visible tests, Submit all 10.