Maximum Product of the Length of Two Palindromic Subsequences - Problem

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

Return the maximum possible product of the lengths of the two palindromic subsequences.

A subsequence 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 palindromic if it reads the same forward and backward.

Input & Output

Example 1 — Basic Case
$ Input: s = "leetcodecom"
Output: 9
💡 Note: One optimal solution: subsequence 1 = "ete" (indices 1,2,7), subsequence 2 = "coc" (indices 4,5,10). Both are palindromes with lengths 3 and 3, giving product 3 × 3 = 9.
Example 2 — Short String
$ Input: s = "bb"
Output: 1
💡 Note: The two subsequences must be disjoint, so we can only take one 'b' for each subsequence. Each has length 1, so the product is 1 × 1 = 1.
Example 3 — No Valid Palindromes
$ Input: s = "accbcaxxcxx"
Output: 25
💡 Note: One optimal solution: subsequence 1 = "accca" (indices 0,1,2,4,5), subsequence 2 = "xxcxx" (indices 6,7,8,9,10). Both are palindromes with lengths 5 and 5, giving product 5 × 5 = 25.

Constraints

  • 2 ≤ s.length ≤ 12
  • s consists of lowercase English letters

Visualization

Tap to expand
Maximum Product of Palindromic SubsequencesInput: "leetcodecom"Indices:012345678910leetcodecomSubseq 1: "ete"Subseq 2: "coc"Indices: 1,2,7Indices: 4,8,10Product: 3 × 3 = 9Both subsequences are palindromes and disjoint
Understanding the Visualization
1
Input String
Given string 'leetcodecom' with characters to assign
2
Split Assignment
Assign each character to subsequence 1, subsequence 2, or skip
3
Validate & Calculate
Check palindromes and find maximum product
Key Takeaway
🎯 Key Insight: Use backtracking to assign each character to one of two subsequences or skip it, then verify palindromes
Asked in
Google 15 Amazon 12 Microsoft 8
35.7K 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