<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 {
private:

    int solve2(string s,vector<vector<int>>&palindromes)
    {
        int n=s.length();
        vector<int>dp(n+1,0);
        dp[n]=-1;
        for(int start=n-1;start>=0;start--)
        {
            string temp="";
            int ans=INT_MAX;
            for(int i=start;i<n;i++)
            {
                if(palindromes[start][i]==1)
                {
                    ans=min(ans,1+dp[i+1]);
                }
            }
            dp[start]=ans;
        }
        return dp[0];
    }

    void findpalindromes(string s,vector<vector<int>>&palindromes)
    {
        // cout<<"HI"<<endl;
        int n=s.length();
        for(int i=0;i<n;i++)
        {
            int prev=i;
            int next=i;
            while(prev>=0 && next<n && s[prev]==s[next])
            {
                palindromes[prev][next]=1;
                prev--;
                next++;
            }
        }
        // cout<<"YO"<<endl;

        for(int i=0;i<n;i++)
        {
            int first=i;
            int second=i+1;
            while(first>=0 && second<n && s[first]==s[second])
            {
                palindromes[first][second]=1;
                first--;
                second++;
            }
        }
        // cout<<"BYE"<<endl;
    }

public:
    int minCut(string s) {
        int n=s.length();
        vector<vector<int>>palindromes(n,vector<int>(n,0));
        findpalindromes(s,palindromes);
        // for(int i=0;i<n;i++)
        // {
        //     for(int j=0;j<n;j++)
        //     {
        //         if(palindromes[i][j]==1)
        //         {
        //             cout<<i<<" "<<j<<endl;
        //         }
        //     }
        // }
        return solve2(s,palindromes);
    }
};
61504