<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 {
public:
    int dp[2001];
    vector<vector<int>>m;
    /*bool palin(string &s,int st,int en){
        int len=en-st+1;
        string t="";
        for(int i=st;i<=en;i++) t.push_back(s[i]);
        for(int i=0;i<t.size()/2;i++){
            if(t[i]!=t[t.size()-i-1]) return false;
        }
        return true;
    }*/
    bool check(string &t){
        for(int i=0;i<t.size()/2;i++){
            if(t[i]!=t[t.size()-i-1]) return false;
        }
        return true;
    }
    int solve(int ind,string &s){
        if(ind>=s.size()) return 0;
        if(dp[ind]!=-1) return dp[ind];
        int ans=1e9;
        for(int i=ind;i<s.size();i++){
            if(m[ind][i]==1){
                int cost=1+solve(i+1,s);
                ans=min(ans,cost);
            }
        }
        return dp[ind]=ans;
    }
    int minCut(string s) {
        memset(dp,-1,sizeof(dp));
        int n=s.size();m.resize(n+1,vector<int>(n+1));
        for(int i=0;i<s.size();i++){
            string t="";
            for(int j=i;j<s.size();j++){
                t.push_back(s[j]);
                if(check(t)) m[i][j]=1;
                else m[i][j]=0;
            }
        }
        return solve(0,s)-1;
    }
};
78015