Strings String basics
130. Reverse Words in a String
You are given a string s made of words and spaces. A word is a run of characters with no space in it.
Return the words of s in reverse order, with one space between each pair of neighbouring words.
s can have spaces before its first word, spaces after its last word, and more than one space between two
words. None of these extra spaces belong in the answer. The answer has exactly one space between two words
and no space at either end. Each word keeps its own characters in their original order.
Answers are shown in double quotes, so you can see that nothing extra is left at either end.
Example 1
Input: s = "practice makes progress"
"progress makes practice"Explanation: The three words, read from last to first, are "progress", "makes" and "practice".
Example 2
Input: s = " north east south "
"south east north"Explanation: The spaces before "north" and after "south" are dropped. The two spaces between "north" and "east" become one.
Example 3
Input: s = "Gate 7B opens at 9"
"9 at opens 7B Gate"Explanation: Capital letters and digits are part of the words. "7B" stays "7B": only the order of the words changes.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ s.length ≤ 105sholds only English letters (lowercase and uppercase), digits and spaces' 'sholds at least one word
Clarifying questions
What you would ask an interviewer before writing any code.