We use a helper function with two pointers l and r. We swap s[l] and s[r] and then recurse for the inner part of the string.
- Helper function
solve(l, r):- Base case: If
l >= r, return. - Swap
s[l]ands[r]. solve(l + 1, r - 1).
- Base case: If
- Call
solve(0, len(s) - 1).
- Time Complexity: O(N).
- Space Complexity: O(N) (stack).
def reverse_string(s):
def solve(l, r):
if l >= r:
return
s[l], s[r] = s[r], s[l]
solve(l + 1, r - 1)
solve(0, len(s) - 1)