<p>Given a string <code>s</code>, find two <strong>disjoint palindromic subsequences</strong> of <code>s</code> such that the <strong>product</strong> of their lengths is <strong>maximized</strong>. The two subsequences are <strong>disjoint</strong> if they do not both pick a character at the same index.</p>

<p>Return <em>the <strong>maximum</strong> possible <strong>product</strong> of the lengths of the two palindromic subsequences</em>.</p>

<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters. A string is <strong>palindromic</strong> if it reads the same forward and backward.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="example-1" src="https://assets.leetcode.com/uploads/2021/08/24/two-palindromic-subsequences.png" style="width: 550px; height: 124px;" />
<pre>
<strong>Input:</strong> s = &quot;leetcodecom&quot;
<strong>Output:</strong> 9
<strong>Explanation</strong>: An optimal solution is to choose &quot;ete&quot; for the 1<sup>st</sup> subsequence and &quot;cdc&quot; for the 2<sup>nd</sup> subsequence.
The product of their lengths is: 3 * 3 = 9.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;bb&quot;
<strong>Output:</strong> 1
<strong>Explanation</strong>: An optimal solution is to choose &quot;b&quot; (the first character) for the 1<sup>st</sup> subsequence and &quot;b&quot; (the second character) for the 2<sup>nd</sup> subsequence.
The product of their lengths is: 1 * 1 = 1.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;accbcaxxcxx&quot;
<strong>Output:</strong> 25
<strong>Explanation</strong>: An optimal solution is to choose &quot;accca&quot; for the 1<sup>st</sup> subsequence and &quot;xxcxx&quot; for the 2<sup>nd</sup> subsequence.
The product of their lengths is: 5 * 5 = 25.
</pre>

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

<ul>
	<li><code>2 &lt;= s.length &lt;= 12</code></li>
	<li><code>s</code> consists of lowercase English letters only.</li>
</ul>

class Solution:
    def magic(self,arr,i,j,dp):
        if i>j:
            return 0
        take, skip1, skip2=0,0,0
        if arr[i]==arr[j]:
            if i==j:
                take=1+self.magic(arr,i+1,j-1,dp)
            else:
                take=2+self.magic(arr,i+1,j-1,dp)
        skip1=self.magic(arr,i+1,j,dp)
        skip2=self.magic(arr,i,j-1,dp)
        return max(take,skip1,skip2)

    def recfun(self,s,i,j,arr,dp):
        if i > j:
            return [arr[:]]
        ans = []
        take, skip1, skip2 = [], [], []
        if s[i] == s[j]:
            if i == j:
                x=arr[:]
                x.append(i)
                take = self.recfun(s, i + 1, j - 1, x[:], dp)
            else:
                x=arr[:]
                x.append(i)
                x.append(j)
                take = self.recfun(s, i + 1, j - 1, x[:], dp)
        skip1 = self.recfun(s, i + 1, j, arr[:], dp)
        skip2 = self.recfun(s, i, j - 1, arr[:], dp)
        for item in take:
            if item:
                ans.append(item)
        for item in skip1:
            if item:
                ans.append(item)
        for item in skip2:
            if item:
                ans.append(item)
        return ans[:]
    def maxProduct(self, s: str) -> int:
        ans = 0
        dp = []
        list_ = self.recfun(s, 0, len(s) - 1, [], dp)
        visited=set()
        new_arr=[]
        for item in list_:
            if tuple(item) not in visited:
                new_arr.append(item)
            visited.add(tuple(item))
        # print(new_arr)
        word=[]
        for char in s:
            word.append(char)
        ans=0
        for loc in new_arr:
            idx=loc[:]
            idx.sort()
            arr=word[:]
            for i in range(len(idx)-1,-1,-1):
                arr.pop(idx[i])
            # print(arr)
            len_=self.magic(arr,0,len(arr)-1,dp)
            ans=max(ans,len(idx)*len_)
        return ans
2987