{"id":8737,"date":"2021-11-20T13:50:07","date_gmt":"2021-11-20T21:50:07","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8737"},"modified":"2021-11-20T14:21:49","modified_gmt":"2021-11-20T22:21:49","slug":"leetcode-2002-maximum-product-of-the-length-of-two-palindromic-subsequences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-2002-maximum-product-of-the-length-of-two-palindromic-subsequences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2002. Maximum Product of the Length of Two Palindromic Subsequences"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>, find two&nbsp;<strong>disjoint palindromic subsequences<\/strong>&nbsp;of&nbsp;<code>s<\/code>&nbsp;such that the&nbsp;<strong>product<\/strong>&nbsp;of their lengths is&nbsp;<strong>maximized<\/strong>. The two subsequences are&nbsp;<strong>disjoint<\/strong>&nbsp;if they do not both pick a character at the same index.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;possible&nbsp;<strong>product<\/strong>&nbsp;of the lengths of the two palindromic subsequences<\/em>.<\/p>\n\n\n\n<p>A&nbsp;<strong>subsequence<\/strong>&nbsp;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&nbsp;<strong>palindromic<\/strong>&nbsp;if it reads the same forward and backward.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/08\/24\/two-palindromic-subsequences.png\" alt=\"example-1\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"leetcodecom\"\n<strong>Output:<\/strong> 9\n<strong>Explanation<\/strong>: An optimal solution is to choose \"ete\" for the 1<sup>st<\/sup> subsequence and \"cdc\" for the 2<sup>nd<\/sup> subsequence.\nThe product of their lengths is: 3 * 3 = 9.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"bb\"\n<strong>Output:<\/strong> 1\n<strong>Explanation<\/strong>: An optimal solution is to choose \"b\" (the first character) for the 1<sup>st<\/sup> subsequence and \"b\" (the second character) for the 2<sup>nd<\/sup> subsequence.\nThe product of their lengths is: 1 * 1 = 1.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"accbcaxxcxx\"\n<strong>Output:<\/strong> 25\n<strong>Explanation<\/strong>: An optimal solution is to choose \"accca\" for the 1<sup>st<\/sup> subsequence and \"xxcxx\" for the 2<sup>nd<\/sup> subsequence.\nThe product of their lengths is: 5 * 5 = 25.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= s.length &lt;= 12<\/code><\/li><li><code>s<\/code>&nbsp;consists of lowercase English letters only.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: DFS<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(3<sup>n<\/sup>*n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxProduct(string s) {    \n    auto isPalindrom = [](const string& s) {\n      for (int i = 0; i < s.length(); ++i)\n        if (s[i] != s[s.size() - i - 1]) return false;\n      return true;\n    };\n    const int n = s.size();\n    vector<string> ss(3);\n    size_t ans = 0;\n    function<void(int)> dfs = [&](int i) -> void {\n      if (i == n) {\n        if (isPalindrom(ss[0]) && isPalindrom(ss[1]))\n          ans = max(ans, ss[0].size() * ss[1].size());\n        return;\n      }\n      for (int k = 0; k < 3; ++k) {\n        ss[k].push_back(s[i]);\n        dfs(i + 1); \n        ss[k].pop_back();  \n      }      \n    };\n    dfs(0);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Subsets + Bitmask + All Pairs<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(2<sup>2n<\/sup>)<br>Space complexity: O(2<sup>n<\/sup>)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int maxProduct(string s) {    \n    auto isPalindrom = [](const string& s) {\n      for (int i = 0; i < s.length(); ++i)\n        if (s[i] != s[s.size() - i - 1]) return false;\n      return true;\n    };\n    const int n = s.size();\n    unordered_set<int> p;\n    for (int i = 0; i < (1 << n); ++i) {\n      string t;\n      for (int j = 0; j < n; ++j)\n        if (i >> j & 1) t.push_back(s[j]);\n      if (isPalindrom(t))\n        p.insert(i);\n    }\n    int ans = 0;\n    for (int s1 : p)\n      for (int s2 : p) \n        if (!(s1 & s2))\n          ans = max(ans, __builtin_popcount(s1) * __builtin_popcount(s2));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s, find two&nbsp;disjoint palindromic subsequences&nbsp;of&nbsp;s&nbsp;such that the&nbsp;product&nbsp;of their lengths is&nbsp;maximized. The two subsequences are&nbsp;disjoint&nbsp;if they do not both pick a character at the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[33,177,42],"class_list":["post-8737","post","type-post","status-publish","format-standard","hentry","category-searching","tag-dfs","tag-medium","tag-search","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8737"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8737\/revisions"}],"predecessor-version":[{"id":8742,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8737\/revisions\/8742"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}