Medium · Strings

Reverse Words Preserving Spaces

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

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Strings problems