EasyTwo Pointers
Reverse String
Reverse character array in-place
Solution Approach
Two pointers approach. Swap characters from both ends moving towards middle.
Implementation
def reverseString(s):
left, right = 0, len(s) - 1
while left < right:
s[left], s[right] = s[right], s[left]
left += 1
right -= 1Complexity Analysis
Time Complexity
O(n)Space Complexity
O(1)Complexity
Time:O(n)
Space:O(1)
Asked at
GoogleAmazonFacebook