Skip to content
RED SAGSkills
easy15 ptsstrings

Reverse Word Order

Given a sentence s, return a new string with its words in reverse order.

A word is a run of non-space characters. The input may have leading, trailing or repeated spaces; the output must have words separated by exactly one space and no spaces at the ends.

Example 1

Input: s = "ship the build"
Output: "build the ship"

Example 2

Input: s = "  fast   tests  "
Output: "tests fast"

Constraints

  • 0 <= s.length <= 10^4
  • s contains printable ASCII characters.
Hint 1

Split on spaces and drop the empty pieces.

Hint 2

Reverse the list of words and join with a single space.

Good practice for: Frontend Developer, Backend Developer, Full Stack Developer, Mobile App Developer, Game Developer, Automation Test Engineer

Run checks your code on 2 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.