{"id":7480,"date":"2020-10-10T23:05:13","date_gmt":"2020-10-11T06:05:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7480"},"modified":"2020-10-11T16:28:03","modified_gmt":"2020-10-11T23:28:03","slug":"leetcode-1616-split-two-strings-to-make-palindrome","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1616-split-two-strings-to-make-palindrome\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1616. Split Two Strings to Make Palindrome"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-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 1616. Split Two Strings to Make Palindrome - \u5237\u9898\u627e\u5de5\u4f5c EP361\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/9Krnwi6OpnU?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>a<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;of the same length. Choose an index and split both strings&nbsp;<strong>at the same index<\/strong>, splitting&nbsp;<code>a<\/code>&nbsp;into two strings:&nbsp;<code>a<sub>prefix<\/sub><\/code>&nbsp;and&nbsp;<code>a<sub>suffix<\/sub><\/code>&nbsp;where&nbsp;<code>a = a<sub>prefix<\/sub>&nbsp;+ a<sub>suffix<\/sub><\/code>, and splitting&nbsp;<code>b<\/code>&nbsp;into two strings:&nbsp;<code>b<sub>prefix<\/sub><\/code>&nbsp;and&nbsp;<code>b<sub>suffix<\/sub><\/code>&nbsp;where&nbsp;<code>b = b<sub>prefix<\/sub>&nbsp;+ b<sub>suffix<\/sub><\/code>. Check if&nbsp;<code>a<sub>prefix<\/sub>&nbsp;+ b<sub>suffix<\/sub><\/code>&nbsp;or&nbsp;<code>b<sub>prefix<\/sub>&nbsp;+ a<sub>suffix<\/sub><\/code>&nbsp;forms a palindrome.<\/p>\n\n\n\n<p>When you split a string&nbsp;<code>s<\/code>&nbsp;into&nbsp;<code>s<sub>prefix<\/sub><\/code>&nbsp;and&nbsp;<code>s<sub>suffix<\/sub><\/code>, either&nbsp;<code>s<sub>suffix<\/sub><\/code>&nbsp;or&nbsp;<code>s<sub>prefix<\/sub><\/code>&nbsp;is allowed to be empty. For example, if&nbsp;<code>s = \"abc\"<\/code>, then&nbsp;<code>\"\" + \"abc\"<\/code>,&nbsp;<code>\"a\" + \"bc\"<\/code>,&nbsp;<code>\"ab\" + \"c\"<\/code>&nbsp;, and&nbsp;<code>\"abc\" + \"\"<\/code>&nbsp;are valid splits.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code><em>&nbsp;if it is possible to form<\/em><em>&nbsp;a palindrome string, otherwise return&nbsp;<\/em><code>false<\/code>.<\/p>\n\n\n\n<p><strong>Notice<\/strong>&nbsp;that&nbsp;<code>x + y<\/code>&nbsp;denotes the concatenation of strings&nbsp;<code>x<\/code>&nbsp;and&nbsp;<code>y<\/code>.<\/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> a = \"x\", b = \"y\"\n<strong>Output:<\/strong> true\n<strong>Explaination:<\/strong> If either a or b are palindromes the answer is true since you can split in the following way:\na<sub>prefix<\/sub> = \"\", a<sub>suffix<\/sub> = \"x\"\nb<sub>prefix<\/sub> = \"\", b<sub>suffix<\/sub> = \"y\"\nThen, a<sub>prefix<\/sub> + b<sub>suffix<\/sub> = \"\" + \"y\" = \"y\", which is a palindrome.\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> a = \"abdef\", b = \"fecab\"\n<strong>Output:<\/strong> true\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> a = \"ulacfd\", b = \"jizalu\"\n<strong>Output:<\/strong> true\n<strong>Explaination:<\/strong> Split them at index 3:\na<sub>prefix<\/sub> = \"ula\", a<sub>suffix<\/sub> = \"cfd\"\nb<sub>prefix<\/sub> = \"jiz\", b<sub>suffix<\/sub> = \"alu\"\nThen, a<sub>prefix<\/sub> + b<sub>suffix<\/sub> = \"ula\" + \"alu\" = \"ulaalu\", which is a palindrome.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> a = \"xbdef\", b = \"xecab\"\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= a.length, b.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>a.length == b.length<\/code><\/li><li><code>a<\/code>&nbsp;and&nbsp;<code>b<\/code>&nbsp;consist of lowercase English letters<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1616-ep361.png\" alt=\"\" class=\"wp-image-7491\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1616-ep361.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1616-ep361-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/10\/1616-ep361-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Try to match the prefix of A and suffix of B (or the other way) as much as possible and then check whether the remaining part is a palindrome or not.<\/p>\n\n\n\n<p>e.g. A = &#8220;abcxyzzz&#8221;, B = &#8220;uuuvvcba&#8221;<br>A&#8217;s prefix abc matches B&#8217;s suffix cba<br>We just need to check whether &#8220;xy&#8221; or &#8220;vv&#8221; is palindrome or not. <br>The concatenated string &#8220;abc|vvcba&#8221; is a palindrome, left abc is from A and vvcba is from B.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/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  bool checkPalindromeFormation(string a, string b) {\n    auto isPalindrome = [](const string& s, int i, int j) {\n      while (i < j &#038;&#038; s[i] == s[j]) ++i, --j;        \n      return i >= j;\n    };\n    auto check = [&isPalindrome](const string& a, const string& b) {\n      int i = 0;\n      int j = a.length() - 1;\n      while (a[i] == b[j]) ++i, --j;\n      return isPalindrome(a, i, j) || isPalindrome(b, i, j);\n    };\n    return check(a, b) || check(b, a);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings&nbsp;a&nbsp;and&nbsp;b&nbsp;of the same length. Choose an index and split both strings&nbsp;at the same index, splitting&nbsp;a&nbsp;into two strings:&nbsp;aprefix&nbsp;and&nbsp;asuffix&nbsp;where&nbsp;a = aprefix&nbsp;+ asuffix, and&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,177,95,4],"class_list":["post-7480","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","tag-palindrome","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7480","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=7480"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7480\/revisions"}],"predecessor-version":[{"id":7492,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7480\/revisions\/7492"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}