Reverse String

LeetCode

LeetCode: https://leetcode.com/problems/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.

1def reverseString(s: List[str]) -> None:
2 def helper(l: int, r: int) -> None:
3 if l >= r:
4 return
5 s[l], s[r] = s[r], s[l]
6 helper(l + 1, r - 1)
7
8 helper(0, len(s) - 1)
l
1
0
2
1
3
2
4
3
r
5
4
Step 1 / 3
Step 1:
Swap the ends (l=0, r=4), then recurse inward.
Pointers: l=0, r=4
Focus: swap @ [0, 4]