I want to verify my assumptions about Time and Space complexity of two different implementations of valid palindrome functions in JavaScript.
In the first implementation we are using a helper function and just pass pointers
const isPalindrome = str => {
return isPalindromeHelper(str, 0, str.length - 1);
}
const isPalindromeHelper = (str, start, end) => {
if (end - start < 1) return true;
return str[start] === str[end] && isPalindromeHelper(str, start + 1, end - 1)
}
In this case I am assuming that the time complexity is O(N) and the space complexity is O(N) as well.
However, let's say that instead of passing pointers we are slicing the string each time. And let's say that slice is O(n) operation.
const isPalindrome = str => {
if (str.length <= 1) return true;
if (str[0] !== str[str.length - 1]) return false;
return isPalindrome(str.slice(1, str.length - 1));
}
Would this push both Time and Space complexity to O(N^2) if slice was O(N) operation? Time because of time complexity of slice and Space would increase since we are constantly creating new strings?