2002. Maximum Product of the Length of Two Palindromic Subsequences LeetCode Solution

In this guide, you will get 2002. Maximum Product of the Length of Two Palindromic Subsequences LeetCode Solution with the best time and space complexity. The solution to Maximum Product of the Length of Two Palindromic Subsequences problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Product of the Length of Two Palindromic Subsequences solution in C++
  4. Maximum Product of the Length of Two Palindromic Subsequences solution in Java
  5. Maximum Product of the Length of Two Palindromic Subsequences solution in Python
  6. Additional Resources
2002. Maximum Product of the Length of Two Palindromic Subsequences LeetCode Solution image

Problem Statement of Maximum Product of the Length of Two Palindromic Subsequences

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.

Example 1:

Input: s = “leetcodecom”
Output: 9
Explanation: 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:

See also  2879. Display the First Three Rows LeetCode Solution

Input: s = “bb”
Output: 1
Explanation: 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
Explanation: 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.

Complexity Analysis

  • Time Complexity: O(n \cdot 3^n)
  • Space Complexity: O(n)

2002. Maximum Product of the Length of Two Palindromic Subsequences LeetCode Solution in C++

class Solution {
 public:
  int maxProduct(string s) {
    size_t ans = 0;
    dfs(s, 0, "", "", ans);
    return ans;
  }

 private:
  void dfs(const string& s, int i, string&& s1, string&& s2, size_t& ans) {
    if (i == s.length()) {
      if (isPalindrome(s1) && isPalindrome(s2))
        ans = max(ans, s1.length() * s2.length());
      return;
    }

    s1.push_back(s[i]);
    dfs(s, i + 1, std::move(s1), std::move(s2), ans);
    s1.pop_back();

    s2.push_back(s[i]);
    dfs(s, i + 1, std::move(s1), std::move(s2), ans);
    s2.pop_back();

    dfs(s, i + 1, std::move(s1), std::move(s2), ans);
  }

  bool isPalindrome(const string& s) {
    int i = 0;
    int j = s.length() - 1;
    while (i < j) {
      if (s[i] != s[j])
        return false;
      ++i;
      --j;
    }
    return true;
  }
};
/* code provided by PROGIEZ */

2002. Maximum Product of the Length of Two Palindromic Subsequences LeetCode Solution in Java

class Solution {
  public int maxProduct(String s) {
    dfs(s, 0, new StringBuilder(), new StringBuilder());
    return ans;
  }

  private int ans = 0;

  private void dfs(final String s, int i, StringBuilder sb1, StringBuilder sb2) {
    if (i == s.length()) {
      if (isPalindrome(sb1) && isPalindrome(sb2))
        ans = Math.max(ans, sb1.length() * sb2.length());
      return;
    }

    final int sb1Length = sb1.length();
    dfs(s, i + 1, sb1.append(s.charAt(i)), sb2);
    sb1.setLength(sb1Length);

    final int sb2Length = sb2.length();
    dfs(s, i + 1, sb1, sb2.append(s.charAt(i)));
    sb2.setLength(sb2Length);

    dfs(s, i + 1, sb1, sb2);
  }

  private boolean isPalindrome(StringBuilder sb) {
    int i = 0;
    int j = sb.length() - 1;
    while (i < j) {
      if (sb.charAt(i) != sb.charAt(j))
        return false;
      ++i;
      --j;
    }
    return true;
  }
}
// code provided by PROGIEZ

2002. Maximum Product of the Length of Two Palindromic Subsequences LeetCode Solution in Python

class Solution:
  def maxProduct(self, s: str) -> int:
    ans = 0

    def isPalindrome(s: str) -> bool:
      i = 0
      j = len(s) - 1
      while i < j:
        if s[i] != s[j]:
          return False
        i += 1
        j -= 1
      return True

    @functools.lru_cache(None)
    def dfs(i: int, s1: str, s2: str) -> None:
      nonlocal ans
      if i == len(s):
        if isPalindrome(s1) and isPalindrome(s2):
          ans = max(ans, len(s1) * len(s2))
        return

      dfs(i + 1, s1 + s[i], s2)
      dfs(i + 1, s1, s2 + s[i])
      dfs(i + 1, s1, s2)

    dfs(0, '', '')
    return ans
# code by PROGIEZ

Additional Resources

See also  2108. Find First Palindromic String in the Array LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.