Reverse Words in a String (Leetcode 151)

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

class Solution:
    def reverseWords(self, s: str) -> str:
        
        temp=list(map(str,s.split()))
        # print(temp)
        ans=''
        for i in range(len(temp)-1,-1,-1):
            if(temp[i]!=' '):
                ans=ans+temp[i]+' '
        
        return ans[:len(ans)-1]

Last updated