Medium · Strings

Reverse words

Given a string s of words separated by spaces, return s with its words in reverse order and its leading and trailing spaces removed; each gap between two words keeps its width ("a good day" → "day good a").

Examples

Example 1

{
  "s": "the sky is"
}

Output: "is sky the"

Example 2

{
  "s": "  hi bob  "
}

Output: "bob hi"

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Strings problems