Sitemap

Reverse a String

1 min readJan 13, 2024

--

Problem Statement:

You are given a string s. You need to reverse the string.

Example 1:

Input:
s = Geeks
Output: skeeG

Example 2:

Input:
s = for
Output: rof

Your 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 -= 1

FOLLOW FOR MORE SUCH CONTENT

--

--

Bittu Singh
Bittu Singh

Written by Bittu Singh

Coder 🚀 | 3+ Years 🛠️ | Software Engineer 💻 Experienced in Python, Node.js, React.js, UI/API Testing and Automation. ✨