Recursive two pointers
Time O(n) Space O(n) stackhelper(l, r): if l >= r stop; swap s[l], s[r]; recurse on (l + 1, r − 1). Depth is n / 2.
class Solution {
public void reverseString(char[] s) {
helper(s, 0, s.length - 1);
}
private void helper(char[] s, int l, int r) {
if (l >= r) return;
char t = s[l]; s[l] = s[r]; s[r] = t;
helper(s, l + 1, r - 1);
}
}Verdict: Fine for moderate n; the iterative version from Module 0 uses O(1) space.