leetcode -> 344 -reverse string
344 - Reverse string -
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
sol 1 - Pattern 'Stack' - Using a stack DS , but it uses O(N) extra memory.
sol 2 - 2 Pointer Approach - Using Two Pointers there is no need of Extra Memory 
time complexity -O(N)
space complexity -O(1)
Code -
class Solution:   
    def reverseString(self, s: List[str]) -> None:
        """
        Do not return anything, modify s in-place instead.
        """
        i,j =0,len(s)-1
        while i<j : 
            s[i],s[j]=s[j],s[i]
            i+=1 
            j-=1
        return s 
 

 
    
Top comments (0)