<p>Given a string <code>s</code>, partition <code>s</code> such that every <span data-keyword="substring-nonempty">substring</span> of the partition is a <span data-keyword="palindrome-string">palindrome</span>.</p>

<p>Return <em>the <strong>minimum</strong> cuts needed for a palindrome partitioning of</em> <code>s</code>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;aab&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> The palindrome partitioning [&quot;aa&quot;,&quot;b&quot;] could be produced using 1 cut.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;a&quot;
<strong>Output:</strong> 0
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;ab&quot;
<strong>Output:</strong> 1
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 2000</code></li>
	<li><code>s</code> consists of lowercase English letters only.</li>
</ul>

class Solution {
    vector<vector<optional<bool>>> palindromeMemo;
    vector<int> minCuts;

    int n;
public:
    int minCut(string s) 
    {
        n = s.size();

        palindromeMemo.resize(n, vector<optional<bool>>(n, std::nullopt));
        minCuts = vector(s.size(), -1);

        return backtrack(s, 0);
    }

    int backtrack(string const& s, int startIndex)
    {
        if (startIndex == n - 1 || isPalindrome(s, startIndex, n - 1))
        {
            return 0; // 0 cut needed
        }

        if (minCuts[startIndex] != -1)
        {
            return minCuts[startIndex];
        }

        int minCut = s.size() - 1;
        for (int i = startIndex; i < n; ++i)
        {
            if (isPalindrome(s, startIndex, i))
            {
                int minCutStr = 1 + backtrack(s, i + 1);
                minCut = min(minCutStr, minCut); // need to take min of all endings!!
            }
        }

        minCuts[startIndex] = minCut;
        return minCut;
    }

    // bool isPalindrome(string const& s, int left, int right)
    // {
    //     if (left >= right)
    //     {
    //         return true; // base
    //     }

    //     if (palindromeMemo[left][right].has_value())
    //     {
    //         palindromeMemo[left][right].value();
    //     }

    //     // bool value = false;
    //     // if ()
    //     // {
    //     //     value = isPalindrome(s, left + 1, right - 1);
    //     // }
        
    //     palindromeMemo[left][right] = s[left] == s[right] && isPalindrome(s, left + 1, right - 1);
    //     return palindromeMemo[left][right] == true;
    // }

    bool isPalindrome(string const& s, int left, int right)
    {
        if (left >= right) // this is for the recursive call below
        {
            return true;
        }

        if (palindromeMemo[left][right].has_value())
        {
            //cout << "Hit cache memoPaindrome: " << left << "," << right << " : " << memoPalindrome[left][right].value() << endl;
            return palindromeMemo[left][right].value();
        }

        palindromeMemo[left][right] = (s[left] == s[right]) && isPalindrome(s, left + 1, right - 1);

        return palindromeMemo[left][right] == true;
    }
};
35280