Logo
    Akhil Abraham
    Akhil Abraham
    0344 - Reverse String
    0344 - Reverse String

    0344 - Reverse String

    Difficulty
    Easy
    Language
    Python
    URL
    https://leetcode.com/problems/reverse-string/

    Solution 1 - Two-Pointer Swap

    Write a function that reverses a string in-place. The input is given as an array of characters s. Use two pointers — one starting at the beginning and one at the end — and swap the characters while moving inward until the pointers meet.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(1)SpaceComplexity: O(1)SpaceComplexity:O(1)

    Solution 2 - Recursive Approach

    Recursively swap the outermost characters and move inward. The base case is when the left pointer meets or passes the right pointer.

    TimeComplexity:O(n)TimeComplexity: O(n)TimeComplexity:O(n)
    SpaceComplexity:O(n)SpaceComplexity: O(n)SpaceComplexity:O(n)
    Logo

    ©️ 2020-2026, Akhil Abraham.

    LinkedInGitHubMediumX
    """ 0344.1 - Reverse String - Solution 1 - Two-Pointer Swap """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def reverseString(self, s: List[str]) -> None:
            """Reverse String Function"""
            left, right = 0, len(s) - 1
    
            while left < right:
                s[left], s[right] = s[right], s[left]
                left += 1
                right -= 1
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        s1 = ["h", "e", "l", "l", "o"]
        Solution().reverseString(s1)
        print(s1)  # ["o", "l", "l", "e", "h"]
    
        s2 = ["H", "a", "n", "n", "a", "h"]
        Solution().reverseString(s2)
        print(s2)  # ["h", "a", "n", "n", "a", "H"]
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()
    
    """ 0344.2 - Reverse String - Solution 2 - Recursive Approach """
    
    #####################################################################################
    # Imports
    #####################################################################################
    from typing import List
    
    #####################################################################################
    # Classes
    #####################################################################################
    class Solution:
        """Solution Class"""
    
        def reverseString(self, s: List[str]) -> None:
            """Reverse String Function"""
            def helper(left: int, right: int) -> None:
                if left >= right:
                    return
                s[left], s[right] = s[right], s[left]
                helper(left + 1, right - 1)
    
            helper(0, len(s) - 1)
    
    
    #####################################################################################
    # Functions
    #####################################################################################
    def testcase():
        """Test Function"""
        s1 = ["h", "e", "l", "l", "o"]
        Solution().reverseString(s1)
        print(s1)  # ["o", "l", "l", "e", "h"]
    
        s2 = ["H", "a", "n", "n", "a", "h"]
        Solution().reverseString(s2)
        print(s2)  # ["h", "a", "n", "n", "a", "H"]
    
    
    #####################################################################################
    # Main
    #####################################################################################
    if __name__ == "__main__":
        testcase()