Maximum Product of The Length of Two Palindromic Subsequences

Medium Topics Company Tags

Choose two nonempty palindromic subsequences of s that use disjoint sets of character indices. Each subsequence preserves the original index order; the two choices may interleave, but they cannot share an index. A palindrome reads the same forward and backward. Return the largest possible product of the two subsequence lengths. You may leave characters unused.

Example 1:

Maximum Product of the Length of Two Palindromic Subsequences, example 1: illustration of the example

Input: s = "abab"

Output: 4

Explanation: Choose the a characters at indices 0 and 2 and the b characters at indices 1 and 3. Both subsequences are palindromes of length 2, so their product is 4.

Example 2:

Maximum Product of the Length of Two Palindromic Subsequences, example 2: illustration of the example

Input: s = "abcdef"

Output: 1

Explanation: Every character is different, so any palindromic subsequence has length 1. Two distinct indices give product 1.

Example 3:

Maximum Product of the Length of Two Palindromic Subsequences, example 3: illustration of the example

Input: s = "aaaaaa"

Output: 9

Explanation: Assign the first three characters to one subsequence and the last three to the other. Both read aaa, giving product 3 × 3 = 9. No split of six available characters can have a larger product.

Constraints:

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


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.