Reverse a String
Problem Statement:
You are given a string s. You need to reverse the string.
Example 1:
Input:
s = Geeks
Output: skeeGExample 2:
Input:
s = for
Output: rofYour Task:
Get Bittu Singh’s stories in your inbox
Join Medium for free to get updates from this writer.
You only need to complete the function reverseWord() that takes s as parameter and returns the reversed string.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).
Constraints:
1 <= |s| <= 10000
Solution:
# User function template for Python 3
class Solution:
def reverseWord(self, str: str) -> str:
# Reverse the given string using slicing (extended slice syntax)
str = str[::-1]
# Return the reversed string
return str
# Driver code
if __name__ == "__main__":
# Take the number of test cases as input
t = int(input())
# Iterate through each test case
while t > 0:
# Take the input string for each test case
s = input()
# Create an instance of the Solution class
ob = Solution()
# Call the reverseWord method and print the result
print(ob.reverseWord(s))
# Decrement the number of test cases
t -= 1FOLLOW FOR MORE SUCH CONTENT










