<p>Given a string <code>s</code>, return <code>true</code> <em>if the </em><code>s</code><em> can be palindrome after deleting <strong>at most one</strong> character from it</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;aba&quot;
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abca&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> You could delete the character &#39;c&#39;.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abc&quot;
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

/**
    understanding
        determine if can be made a valid palindrome by removing 0 or 1 characters
        basically comes down to checking recursively:
            is string pal
            is string with left removed pal
            is string with right removed pal

        optimization
            if detect a palindrome, return true
            if detect non-palindrome, return false but cache (l, r) => false

*/

// "abc"


class Solution {
    bool isPal(const string& s, int left, int right, bool canDelete) {
        if (left >= right) return true;

        bool result = s[left] == s[right] && isPal(s, left + 1, right - 1, canDelete);
        if (result) return true;

        if (canDelete) {
            result = result || isPal(s, left + 1, right, false);
            if (result) return true;

            result = result || isPal(s, left, right - 1, false);            
        }
        return result;
    }
public:
    bool validPalindrome(string s) {
        return isPal(s, 0, s.length() - 1, true);
    }
};
20946