Reverse String

Write a function that takes a string as input and returns the string reversed.

Example: Given s = "hello", return "olleh".

class Solution(object):
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        """
        return s[::-1]
        """
        l = list(s)
        start = 0
        end = len(l)-1
        while start < end:
            l[start],l[end] = l[end],l[start]
            start += 1
            end -= 1
        return ''.join(l)
class Solution(object):
    def reverseString(self, s):
        l = len(s)
        if l < 2:
            return s
        return self.reverseString(s[l/2:]) + self.reverseString(s[:l/2])

Last updated

Was this helpful?