<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 maxProduct(self, s: str) -> int:
        resChar = set()
        resIdx = set()

        def backtrack(start, pathChar, pathIdx):
            if pathChar and pathChar == pathChar[::-1]:
                resChar.add(tuple(pathChar[:]))
                resIdx.add(tuple(pathIdx[:]))

            for i in range(start, len(s)):
                backtrack(i + 1, pathChar + [s[i]], pathIdx + [i])

        backtrack(0, [], [])
        resIdx.discard(tuple(i for i in range(len(s))))

        # Convert the list of palindrome indices to bitmasks
        def to_bitmask(indices):
            bitmask = 0
            for idx in indices:
                bitmask |= (1 << idx)  # Set the corresponding bit for each index
            return bitmask

        # Store each palindrome's bitmask and its length
        palindromes = [(to_bitmask(indices), len(indices)) for indices in resIdx]

        res = 0

        # Check each palindrome for overlaps and compute the maximum product
        for mask1, len1 in palindromes:
            non_overlapping = [len2 for mask2, len2 in palindromes if mask1 & mask2 == 0]  # Find non-overlapping palindromes

            # Find the longest non-overlapping palindrome (or 1 if none exist)
            if non_overlapping:
                max2 = max(non_overlapping)
            else:
                max2 = 1  # If no non-overlapping palindrome is found, consider len 1

            # Update the result with the maximum product of lengths
            res = max(res, len1 * max2)

        return res
18400