You recursively call the function n/2 times, where n is the length of the string, since you remove first and last entry of the string at each iteration.
Therefore, the complexity would be O(n/2)
= O(n)
, and you have at most 3 operations at each function call. That would multiply all this by a constant, which does not change the complexity.
EDIT: as noted in comments and another answer, one of these operations is string.slice
. You need to check for the complexity of this, because it will multiply as well and can change overall complexity. If slice
is O(1) constant, then overall you'll have O(n). If slice
is O(n), then overall that would be O(n^2).
For space complexity, you create a lot of arrays. I'll let you the details, but I'm tempted to say it's O(n^2)
at first sight.
Sketch of calculations : first array is size n
, then n-2
, n-4
, sum that all up with some math summation formulas.
hint : n + (n-1) + (n-2) + ...
is n * (n + 1) / 2
which is O(n^2)
, that should give enough "feel" that this is O(n^2)
too.