LeetCode Problem

How to Solve Maximum Product of the Length of Two Palindromic Subsequences

The solution explores all possible pairs of disjoint subsequences and calculates the product of their palindromic lengths. Using bitmasking and state transition dynamic programming allows efficient evaluation despite exponential subsequence combinations. This ensures the maximum product is identified without repeatedly checking overlapping indices.

GhostInterview Help

Need help with Maximum Product of the Length of Two Palindromic Subsequences without spending extra time grinding it?

GhostInterview can read Maximum Product of the Length of Two Palindromic Subsequences from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #2002State transition dynamic programmingReviewed 2026-03-08
Difficulty
Medium
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The solution explores all possible pairs of disjoint subsequences and calculates the product of their palindromic lengths. Using bitmasking and state transition dynamic programming allows efficient evaluation despite exponential subsequence combinations. This ensures the maximum product is identified without repeatedly checking overlapping indices.

Problem Statement

Given a string s, determine two non-overlapping subsequences that are both palindromic, such that their length product is as large as possible. Characters cannot be shared between the two subsequences.

Return the highest achievable product of the lengths of these two palindromic subsequences. A subsequence keeps character order from s, and a palindrome reads the same forward and backward.

Examples

Example 1

Input: s = "leetcodecom"

Output: 9

An optimal solution is to choose "ete" for the 1st subsequence and "cdc" for the 2nd subsequence. The product of their lengths is: 3 * 3 = 9.

Example 2

Input: s = "bb"

Output: 1

An optimal solution is to choose "b" (the first character) for the 1st subsequence and "b" (the second character) for the 2nd subsequence. The product of their lengths is: 1 * 1 = 1.

Example 3

Input: s = "accbcaxxcxx"

Output: 25

An optimal solution is to choose "accca" for the 1st subsequence and "xxcxx" for the 2nd subsequence. The product of their lengths is: 5 * 5 = 25.

Constraints

  • 2 <= s.length <= 12
  • s consists of lowercase English letters only.

Solution Approach

Generate all subsequences using bitmasking

Use bitmasks to represent every possible subsequence of s. Each bit indicates whether a character is included, allowing efficient enumeration of all combinations while preserving original order.

Check palindromic subsequences and disjointness

For each pair of subsequences, verify they are palindromic and have no overlapping indices. Use bitwise AND to quickly detect shared characters between two bitmasks.

Maximize product using dynamic programming

Store lengths of palindromic subsequences for each mask. Iterate over all mask pairs to compute the product of their lengths, updating a running maximum to find the final result.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(2^n * 2^n * n) in the worst case due to enumerating all subsequence pairs and checking palindromes of length n. Space complexity is O(2^n * n) to store palindrome lengths for each subsequence mask.

What Interviewers Usually Probe

  • Can you generate all subsequences and efficiently check for palindromes?
  • Consider bitmasking to represent subsequences and check disjoint characters quickly.
  • Can you optimize by storing palindromic lengths to avoid repeated computation?

Common Pitfalls or Variants

Common pitfalls

  • Failing to ensure the two subsequences are disjoint by index.
  • Rechecking palindrome property repeatedly instead of caching results.
  • Overlooking subsequences that maximize product due to greedy selection of longest palindromes first.

Follow-up variants

  • Find the maximum sum of lengths instead of product for two disjoint palindromic subsequences.
  • Allow three disjoint palindromic subsequences and maximize the product of their lengths.
  • Restrict subsequences to a specific minimum length and compute the maximum product.

How GhostInterview Helps

  • Automatically enumerates all possible disjoint subsequence pairs and validates palindromes efficiently.
  • Caches palindromic lengths for rapid evaluation of product combinations to find the maximum.
  • Provides step-by-step reasoning to avoid common pitfalls like overlapping indices or redundant checks.

Topic Pages

FAQ

What is the main pattern used in Maximum Product of the Length of Two Palindromic Subsequences?

The problem primarily uses state transition dynamic programming combined with bitmasking to handle all subsequence combinations efficiently.

Can this problem be solved with simple recursion?

Recursion alone is too slow due to exponential subsequence pairs; bitmasking with DP is needed for acceptable performance.

Why is bitmasking useful here?

Bitmasking allows efficient representation of subsequences and quick checks for disjoint characters using bitwise operations.

What is the maximum input size to consider?

The string length is constrained between 2 and 12 characters, so enumerating all subsequences is feasible.

How do you check if a subsequence is a palindrome efficiently?

Store previously computed palindromic lengths or use a two-pointer check on the subsequence represented by a bitmask.

GhostInterview Solver

Need direct help with Maximum Product of the Length of Two Palindromic Subsequences instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Maximum Product of the Length of Two Palindromic Subsequences from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.