151. Reverse words in a string

Given an input string, reverse the string word by word.

Example 1: Input: “the sky is blue” Output: “blue is sky the”

  • code, [::-1] or reversed
class Solution:
    def reverseWords(self, s: str) -> str:
        return " ".join(reversed(s.strip().split()))