Menu
CoddyTech

Reverse Words in a String

You get a string s made of words and spaces. A word is a run of letters and digits with no space inside it. Words can be separated by more than one space, and s can start or end with spaces.

Return the words in reverse order, joined by exactly one space, with no space at the start or the end. Each word keeps its spelling; only the order of the words changes.

Function

reverseWords(s: string) → string
sstring
letters, digits and spaces holding at least one word
Returnsstring
the words of s in reverse order, separated by single spaces

Constraints

  • 1 ≤ s.length ≤ 104
  • s contains only English letters, digits and spaces.
  • s holds at least one word.

Examples

Input
s = "Coddy runs in 15 languages"
Output
"languages 15 in runs Coddy"
Explanation
The words are Coddy, runs, in, 15 and languages. Read from last to first they give languages 15 in runs Coddy; 15 counts as a word because words may hold digits.
challenge icon

Follow-up

Can you reverse the words but keep the spacing of s: the same runs of spaces, with the same lengths, in the same order?

Reset code
def reverseWords(s):
    # Write code here

Test #1

Test #2

Test #3

Argument

s = "Coddy runs in 15 languages"

Expected Output

"languages 15 in runs Coddy"