<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[2002], p[2002][2002];
    // int n;
    // int func(string &s, int i){
        
    // }

    int minCut(string s) {
        int n = s.size();
        int dp[n][n];
        memset(dp, 0, sizeof(dp));
        for (int i=0; i<n; i++) dp[i][i] = 1;
        for (int i=0; i<n-1; i++){
            if (s[i] == s[i+1]) dp[i][i+1] = 1;
            else dp[i][i+1] = 0;
        }

        for (int i=2; i<n; i++){
            for (int j=0; i+j<n; j++){
                // cout << j << " " << i+j << " " << s[i] << " " << s[i+j] << endl;
                if (s[j] == s[i+j]) dp[j][i+j] = dp[j+1][i+j-1];
                else dp[j][i+j] = 0;
            }
        }

        // for (int i=0; i<n; i++){
        //     for (int j=0; j<n; j++){
        //         cout << dp[i][j] << " ";
        //     }
        //     cout << endl;
        // }

        int arr[n+1];
        for (int i=0; i<=n; i++) arr[i] = 1e9;
        arr[n-1] = 1;
        arr[n] = 0;
        for (int i=n-2; i>=0; i--){
            for (int j=i; j<n; j++){
                if (dp[i][j]) arr[i] = min(arr[i], 1 + arr[j+1]);
            }
        }
        return arr[0] - 1;
    }
};
24596