{"id":8152,"date":"2021-02-21T00:45:45","date_gmt":"2021-02-21T08:45:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8152"},"modified":"2021-02-28T10:45:23","modified_gmt":"2021-02-28T18:45:23","slug":"leetcode-1771-maximize-palindrome-length-from-subsequences","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1771-maximize-palindrome-length-from-subsequences\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1771. Maximize Palindrome Length From Subsequences"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1771. Maximize Palindrome Length From Subsequences - \u5237\u9898\u627e\u5de5\u4f5c EP386\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/0dbo8C64UFk?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given two strings,&nbsp;<code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>. You want to construct a string in the following manner:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Choose some&nbsp;<strong>non-empty<\/strong>&nbsp;subsequence&nbsp;<code>subsequence1<\/code>&nbsp;from&nbsp;<code>word1<\/code>.<\/li><li>Choose some&nbsp;<strong>non-empty<\/strong>&nbsp;subsequence&nbsp;<code>subsequence2<\/code>&nbsp;from&nbsp;<code>word2<\/code>.<\/li><li>Concatenate the subsequences:&nbsp;<code>subsequence1 + subsequence2<\/code>, to make the string.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>length<\/strong>&nbsp;of the longest&nbsp;<strong>palindrome<\/strong>&nbsp;that can be constructed in the described manner.&nbsp;<\/em>If no palindromes can be constructed, return&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<p>A&nbsp;<strong>subsequence<\/strong>&nbsp;of a string&nbsp;<code>s<\/code>&nbsp;is a string that can be made by deleting some (possibly none) characters from&nbsp;<code>s<\/code>&nbsp;without changing the order of the remaining characters.<\/p>\n\n\n\n<p>A&nbsp;<strong>palindrome<\/strong>&nbsp;is a string that reads the same forward&nbsp;as well as backward.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> word1 = \"cacb\", word2 = \"cbba\"\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> Choose \"ab\" from word1 and \"cba\" from word2 to make \"abcba\", which is a palindrome.<\/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> word1 = \"ab\", word2 = \"ab\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Choose \"ab\" from word1 and \"a\" from word2 to make \"aba\", which is a palindrome.<\/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> word1 = \"aa\", word2 = \"bb\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> You cannot construct a palindrome from the described method, so return 0.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= word1.length, word2.length &lt;= 1000<\/code><\/li><li><code>word1<\/code>&nbsp;and&nbsp;<code>word2<\/code>&nbsp;consist of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1771-ep386.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1771-ep386.png\" alt=\"\" class=\"wp-image-8182\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1771-ep386.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1771-ep386-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1771-ep386-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1771-ep386-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1771-ep386-2.png\" alt=\"\" class=\"wp-image-8183\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1771-ep386-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1771-ep386-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/02\/1771-ep386-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>Similar to <a href=\"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-516-longest-palindromic-subsequence\/\" data-type=\"post\" data-id=\"2860\">\u82b1\u82b1\u9171 LeetCode 516. Longest Palindromic Subsequence<\/a><\/p>\n\n\n\n<p>Let s = word1 + word2, build dp table on s. We just need to make sure there&#8217;s at least one char from each  string.<\/p>\n\n\n\n<p>Time complexity: O((m+n)^2)<br>Space complexity: O((m+n)^2)<\/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 longestPalindrome(string word1, string word2) {\n    const int l1 = word1.length();\n    const int l2 = word2.length();\n    string s = word1 + word2;\n    const int n = l1 + l2;\n    vector<vector<int>> dp(n, vector<int>(n));\n    for (int i = 0; i < n; ++i) dp[i][i] = 1;\n    for (int l = 2; l <= n; ++l)\n      for (int i = 0, j = i + l - 1; j < n; ++i, ++j) {\n        if (s[i] == s[j])\n          dp[i][j] = dp[i + 1][j - 1] + 2;\n        else\n          dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);\n      }\n    \n    int ans = 0;\n    for (int i = 0; i < l1; ++i)\n      for (int j = 0; j < l2; ++j)\n        if (word1[i] == word2[j])\n          ans = max(ans, dp[i][l1 + j]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>O(m+n) Space complexity<\/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 longestPalindrome(string word1, string word2) {\n    const int l1 = word1.length();\n    const int l2 = word2.length();\n    string s = word1 + word2;\n    const int n = l1 + l2;\n    vector<int> dp1(n, 1), dp2(n);\n    int ans = 0;\n    for (int l = 2; l <= n; ++l) {\n      vector<int> dp(n);\n      for (int i = 0, j = i + l - 1; j < n; ++i, ++j) {\n        if (s[i] == s[j]) {\n          dp[i] = dp2[i + 1] + 2;\n          if (i < l1 &#038;&#038; j >= l1)\n            ans = max(ans, dp[i]);\n        } else {\n          dp[i] = max(dp1[i + 1], dp1[i]);\n        }\n      }\n      \/\/ dp2, dp1 = dp1, dp\n      dp1.swap(dp); \n      dp2.swap(dp);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings,&nbsp;word1&nbsp;and&nbsp;word2. You want to construct a string in the following manner: Choose some&nbsp;non-empty&nbsp;subsequence&nbsp;subsequence1&nbsp;from&nbsp;word1. Choose some&nbsp;non-empty&nbsp;subsequence&nbsp;subsequence2&nbsp;from&nbsp;word2. Concatenate the subsequences:&nbsp;subsequence1 + subsequence2, to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[18,95,4,229],"class_list":["post-8152","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-palindrome","tag-string","tag-subsequence","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8152","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=8152"}],"version-history":[{"count":5,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8152\/revisions"}],"predecessor-version":[{"id":8185,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8152\/revisions\/8185"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}