MuhammadTausif / gfg-excercises

This repo is about exercises at GFG
Apache License 2.0
0 stars 0 forks source link

Reverse Words - POTD | 19-Sep-2024 #21

Closed MuhammadTausif closed 6 hours ago

MuhammadTausif commented 6 hours ago

Reverse Words

Link

Difficulty: Easy

Given a String str, reverse the string without reversing its individual words. Words are separated by dots.

Note: The last character has not been '.'.

Examples :

Input: str = i.like.this.program.very.much
Output: much.very.program.this.like.i
Explanation: After reversing the whole string(not individual words), the input string becomes much.very.program.this.like.i
Input: str = pqr.mno
Output: mno.pqr
Explanation: After reversing the whole string , the input string becomes mno.pqr

Constraints:

MuhammadTausif commented 6 hours ago

Solved


def reverseWords(self,str):
        # code here 
        s = str.split('.')
        new_s = s[::-1]

        return ".".join(new_s)