Straight forward algorithm to reverse strings, arrays, lists. Just make sure that you are modifying the memory location, and not just creating a copy.
In Python,
s = s[::-1]
will not work, because you are not modifying the string slice/in-memory location, you are just modifying the pointer that gives access to that string. Uses[:] = s[::-1]
instead.
Code
Python3
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
l, r = 0, len(s)-1
while l < r:
s[l], s[r] = s[r], s[l]
l += 1
r -= 1
Big O Analysis
-
Runtime
The runtime complexity here is since we would have to traverse the entire string.
-
Memory
The memory usage is since we do it in-place.
— A