Reverse String

Desciption

Write a function that reverses a string. The input string is given as an array of characters s.

You must do this by modifying the input array in-place with O(1) extra memory.

Thought

Two Pointer

Code

class Solution:
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        # s.reverse()
        # The List Function is the easiest way
        slow = 0
        while(slow < len(s) - slow - 1):
            tmp = s[slow]
            s[slow] = s[len(s) - slow -1]
            s[len(s) - slow - 1] = tmp
            slow += 1

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *