<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>

int dp[2002][2002];
int dp2[2002][2002];
class Solution {
public:
    bool check(int l, int r, string &s)
    {
        if(l==r)return true;
        if(r-l==1)return s[l]==s[r];
        if(dp[l][r]!=-1)return dp[l][r];
        return dp[l][r]=(s[l]==s[r])&&check(l+1,r-1,s);
    }
    int cuts(int l,int last,string &s)
    {
        if (l == s.size()) {
            if (l == last || check(last, l - 1, s)) return 0;
            else return 1e9;
        }
        if (dp2[l][last] != -1) return dp2[l][last];
        int ans = 1e9;
        for (int i = l; i < s.size(); i++) {
            if (check(last, i, s)) ans = min(ans, 1 + cuts(i + 1, i + 1, s));
        }
        return dp2[l][last] = ans;
    }
    int minCut(string s) {
        memset(dp,-1,sizeof(dp));
        memset(dp2,-1,sizeof(dp2));
        check(0,s.size()-1,s);
        return cuts(0,0,s)-1;
    }
};
37223