<p>Given a string <code>s</code>, return <code>true</code> <em>if it is possible to split the string</em> <code>s</code> <em>into three <strong>non-empty</strong> palindromic substrings. Otherwise, return </em><code>false</code>.​​​​​</p>

<p>A string is said to be palindrome if it the same string when reversed.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;abcbdd&quot;
<strong>Output:</strong> true
<strong>Explanation: </strong>&quot;abcbdd&quot; = &quot;a&quot; + &quot;bcb&quot; + &quot;dd&quot;, and all three substrings are palindromes.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;bcbddxy&quot;
<strong>Output:</strong> false
<strong>Explanation: </strong>s cannot be split into 3 palindromes.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= s.length &lt;= 2000</code></li>
	<li><code>s</code>​​​​​​ consists only of lowercase English letters.</li>
</ul>


class Solution {
public:

   int pref[2001][2001];
    bool palin(string& s) {

        int i = 0, j = s.length() - 1;

        while (i < j) {
            if (s[i] != s[j])

                return false;
            i++;
            j--;
        }
        return true;
    }

  bool pal(string &s,int i,int j)
  {
       

        while (i < j) {
            if (s[i] != s[j])

                return false;
            i++;
            j--;
        }
        return true;
  }
    bool func(vector<vector<int>>& dp, string& s, int i, int cou) {

        if (i >= s.length()) {

            if (cou == 3)
                return true;

            return false;
        }
        if (cou > 3)
            return false;

        if (dp[i][cou] != -1)
            return dp[i][cou];

        int maxm = 0;
        string ss = "";
        for (int j = i; j < s.length(); j++) {

            ss += s[j];
            if (palin(ss)) {

                maxm = (maxm || (func(dp, s, j + 1, cou + 1)));
            }
        }

        return dp[i][cou] = maxm;
    }



   void precal(string &s)
   {

          for(int i=0;i<s.length();i++)
          {
            string s1="",s2="";
            for(int j=i;j<s.length();j++)
            {
                s1=s1+s[j];
                s2=s[j]+s2;

                if(s1==s2)
                   
                   pref[i][j]=1;

                   else pref[i][j]=0;

            }
          }
      

   }

   void isPal(string& s, int left, int right) {


         while(left>=0 && right<s.length() && s[left]==s[right])
         {
            // st.insert(s.substr(left,right+1));
           
           pref[left][right]=1;
           
            left--;
            right++;
         }

    }
    void countSubstrings(string s) {

        for (int i = 0; i < s.length(); i++) {

            isPal(s, i, i);
            isPal(s, i, i + 1);
        }

        
    }
    bool checkPartitioning(string& s) {

      countSubstrings(s);



 

        vector<vector<int>> dp(s.length() + 1, vector<int>(4 + 1, 0));

        dp[s.length()][3] = 1;

        for (int i = s.length()-1; i >=0; i--) {
            for (int cou = 3; cou >=0; cou--) {
                int maxm = 0;
                string ss = "";
                for (int j = i; j < s.length(); j++) {

                   
                    if (pref[i][j]) {

                        maxm = (maxm ||  dp[j + 1][cou+1])  ;
                    }
                }
dp[i][cou] = maxm;
            }
        }

        return dp[0][0]
;
        // return func(dp, s, 0,0);
    }
};
29538