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

#define ll long long
class Solution {
public:

    int minCut(string s) {
        ll n = s.length();
        vector<vector<ll> > v(n);
        for(ll i=0; i<n; i++){
            v[i].push_back(i);
            ll id1 = i-1;
            ll id2 = i+1;
            while(id1>=0 && id2<n){
                if(s[id1]==s[id2]){
                    v[id2].push_back(id1);
                    id1--;
                    id2++;
                }else{
                    break;
                }
            }
        }
        for(ll i=0; i<(n-1); i++){
            if(s[i]!=s[i+1]){
                continue;
            }
            v[i+1].push_back(i);
            ll id1 = i-1;
            ll id2 = i+2;
            while(id1>=0 && id2<n){
                if(s[id1]==s[id2]){
                    v[id2].push_back(id1);
                    id1--;
                    id2++;
                }else{
                    break;
                }
            }
        }
        // for(ll i=0; i<n; i++){
        //     for(ll j=i; j<n; j++){
        //         string f = s.substr(i,j-i+1);
        //         string h = s.substr(i,j-i+1);
        //         reverse(h.begin(),h.end());
        //         if(h==f){
        //             // cout<<h<<" "<<s<<"\n";
        //             v[j].push_back(i);
        //         }
        //     }
        // }
        ll max = 1000000007;
        vector<ll> dp(n+1,max);
        dp[0] = 0;
        for(ll i=1; i<=n; i++){
            ll k = v[i-1].size();
            for(ll j=0; j<k; j++){
                ll st = v[i-1][j];
                ll prev = dp[st];
                if(prev!=max){
                    dp[i] = fmin(dp[i],1+dp[st]);
                }
            }
        }
        // for(ll i=1; i<=n; i++){
        //     ll k = v[i-1].size();
        //     for(ll j=0; j<k; j++){
        //         cout<<v[i-1][j]<<" ";
        //     }cout<<"\n";
        // }
        // for(ll i=0; i<=n; i++){
        //     cout<<dp[i]<<" ";
        // }cout<<"\n";
        // ll ans = -1;
        // recur(v,0,ans,0);
        return dp[n]-1;
    }
};
75101