151. Reverse Words in a String

class Solution:
    def reverseWords(self, s: str) -> str:
        words=[x for x in s.split(' ') if x != '']
        new_str = ''
        for n in range(len(words)):
            new_str += words[len(words)-n-1]
            if n != len(words)-1:
                new_str += ' '
        return new_str

Last updated

Was this helpful?