Maximum Product of the Length of Two Palindromic Subsequences - Problem

Imagine you have a string of characters and need to find two separate palindromic subsequences that don't share any characters at the same positions. Your goal is to maximize the product of their lengths.

Given a string s, you need to:

  • Find two disjoint palindromic subsequences (they cannot pick characters from the same index)
  • Maximize the product of their lengths
  • Return this maximum possible product

Key Concepts:

  • A subsequence maintains the original order but can skip characters
  • A palindrome reads the same forwards and backwards
  • Disjoint means no shared character positions between the two subsequences

Example: For string "leetcodecom", you might pick subsequence "ete" from positions [1,2,7] and "coc" from positions [3,8,10], giving a product of 3 ร— 3 = 9.

Input & Output

example_1.py โ€” Basic Case
$ Input: s = "leetcodecom"
โ€บ Output: 9
๐Ÿ’ก Note: We can select palindromic subsequence "ete" from positions [1,2,7] with length 3, and palindromic subsequence "coco" from positions [3,8,9,10] with length 4. Since these don't share any positions, the product is 3 ร— 3 = 9.
example_2.py โ€” Short String
$ Input: s = "bb"
โ€บ Output: 1
๐Ÿ’ก Note: The only way to create two disjoint palindromic subsequences is to take one 'b' for each subsequence. Each subsequence has length 1, so the product is 1 ร— 1 = 1.
example_3.py โ€” Complex Case
$ Input: s = "accbcaxxcxx"
โ€บ Output: 25
๐Ÿ’ก Note: We can form palindromic subsequence "accca" from positions [0,1,2,5,6] with length 5, and palindromic subsequence "cxcxc" from positions [3,7,8,9,10] with length 5. The product is 5 ร— 5 = 25.

Visualization

Tap to expand
Maximum Product of Palindromic SubsequencesString: "leetcode"l0e1e2t3c4o5d6e7Subsequence 1 (Red): "ete"Positions: [1, 2, 7] โ†’ Length: 3โœ“ Palindrome check: "ete" = "ete"Subsequence 2 (Green): "tcd"Positions: [3, 4, 6] โ†’ Length: 3โœ— Palindrome check: "tcd" โ‰  "dct"Optimal Solution Found:Subsequence 1: "ee" (positions [1,7])Subsequence 2: "tct" (positions [3,4,6])Both are palindromes โœ“Disjoint positions โœ“Product: 2 ร— 3 = 6๐Ÿ”‘ Key Insight: Use bitmasks to represent position setsTwo subsequences are disjoint if mask1 & mask2 == 0Example: 10100010 & 01011000 = 00000000 โœ“ (disjoint)
Understanding the Visualization
1
Choose Flowers
For each flower position, decide: Garden 1, Garden 2, or neither
2
Check Symmetry
Ensure each garden arrangement is palindromic (symmetric)
3
Calculate Score
Multiply the sizes of both gardens to get the total score
4
Find Maximum
Try all possible arrangements and select the one with highest score
Key Takeaway
๐ŸŽฏ Key Insight: Bitmask representation allows efficient checking of disjoint subsequences and systematic exploration of all valid partitions with memoization for optimization.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(4^n)

O(2^n) to generate subsequences, O(n) to check palindrome, O(2^(2n)) to check all pairs

n
2n
โœ“ Linear Growth
Space Complexity
O(2^n)

Store all palindromic subsequences with their bitmasks

n
2n
โš  Quadratic Space

Constraints

  • 2 โ‰ค s.length โ‰ค 12
  • s consists of lowercase English letters only
  • The two palindromic subsequences must be disjoint (no shared character positions)
Asked in
Google 45 Microsoft 32 Amazon 28 Meta 15
18.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen