Reverse Words in a String

Given an input string s, reverse the order of the words. A word is a sequence of non-space characters, and words are separated by spaces. Return a string of the words in reverse order with a single space between them. The string may have leading or trailing spaces or multiple spaces between words, which should be reduced.

Constraints

  • 1 <= s.length <= 10^4
  • s contains English letters (upper-case and lower-case), digits, and spaces ' '.
  • There is at least one word in s.
Saving… · 2 lines
class Solution:
def reverseWords(self, s: str) -> str:
Console

Click Run to execute sample tests.