Here is the leetcode question where you are given a string s and you can delete at most one character to judge whether or not this is a palindrome. For example,
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
So here is my solution using recursion
var validPalindrome = function(s) {
if(s.trim().length === 0) return true
const isValidPalindrome = (start, end, skipped) => {
if(skipped > 1) return false
if(start >= end) return true
if(s[start] === s[end]) return isValidPalindrome(start+1,end-1, skipped)
else return isValidPalindrome(start, end - 1, skipped + 1) || isValidPalindrome(start + 1, end, skipped + 1)
}
return isValidPalindrome(0, s.length - 1, 0)
};
When there's a mismatch between the left character and the right character, you can skip one or the other, but you don't know which one. Therefore, at this point, you simply need to explore both of these paths.
But I am having a hard time analyzing the time complexity for this type of recursion. I know that the iterative version of this solution should have an O(n) runtime. Not sure how I can approach analyzing the time complexity here.