Maximum Product of the Length of Two Palindromic Subsequences LeetCode solution in Python
1class Solution:2def maxProduct(self, s: str) -> int:3# n <= 12, which means the search space is small4n = len(s)5arr = []67for mask in range(1, 1 << n):8subseq = ""9for i in range(n):10# convert the bitmask to the actual subsequence11if mask & (1 << i) > 0:12subseq += s[i]13if subseq == subseq[::-1]:14arr.append((mask, len(subseq)))1516result = 117for (mask1, len1), (mask2, len2) in product(arr, arr):18# disjoint19if mask1 & mask2 == 0:20result = max(result, len1 * len2)21return result