Problem statement

https://leetcode.com/problems/reverse-words-in-a-string-iii/

Solution

Just use build-in functions if it is allowed: split string, reverse each part and then construct back. If it it not allowed, we can use Two Pointers approach.

Complexity

Time and space complexity is O(n).

Code

class Solution:
    def reverseWords(self, s):
        return " ".join(x[::-1] for x in s.split())