{"id":5393,"date":"2019-08-04T11:31:19","date_gmt":"2019-08-04T18:31:19","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5393"},"modified":"2019-08-06T09:33:34","modified_gmt":"2019-08-06T16:33:34","slug":"leetcode-1147-longest-chunked-palindrome-decomposition","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1147-longest-chunked-palindrome-decomposition\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1147. Longest Chunked Palindrome Decomposition"},"content":{"rendered":"\n<p>Return the largest possible&nbsp;<code>k<\/code>&nbsp;such that there exists&nbsp;<code>a_1, a_2, ..., a_k<\/code>&nbsp;such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Each&nbsp;<code>a_i<\/code>&nbsp;is a non-empty string;<\/li><li>Their concatenation&nbsp;<code>a_1 + a_2 + ... + a_k<\/code>&nbsp;is equal to&nbsp;<code>text<\/code>;<\/li><li>For all&nbsp;<code>1 &lt;= i &lt;= k<\/code>,&nbsp;&nbsp;<code>a_i = a_{k+1 - i}<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> text = \"ghiabcdefhelloadamhelloabcdefghi\"\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> We can split the string on \"(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)\".\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> text = \"merchant\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> We can split the string on \"(merchant)\".\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> text = \"antaprezatepzapreanta\"\n<strong>Output:<\/strong> 11\n<strong>Explanation:<\/strong> We can split the string on \"(a)(nt)(a)(pre)(za)(tpe)(za)(pre)(a)(nt)(a)\".\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> text = \"aaa\"\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> We can split the string on \"(a)(a)(a)\".<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Break the string when the shortest palindrome is found.<br>prefer to use string_view<\/p>\n\n\n\n<p>Time complexity: O(n^2)<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, 0 ms, 8.7 ms\nclass Solution {\npublic:\n  int longestDecomposition(string_view text) {    \n    const int n = text.length();\n    if (n == 0) return 0;\n    for (int l = 1; l <= n \/ 2; ++l) {\n      if (text.substr(0, l) == text.substr(n - l)) \n        return 2 + longestDecomposition(text.substr(l, n - 2 * l));\n    }\n    return 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Return the largest possible&nbsp;k&nbsp;such that there exists&nbsp;a_1, a_2, &#8230;, a_k&nbsp;such that: Each&nbsp;a_i&nbsp;is a non-empty string; Their concatenation&nbsp;a_1 + a_2 + &#8230; + a_k&nbsp;is equal to&nbsp;text;&#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,47],"tags":[88,217,95],"class_list":["post-5393","post","type-post","status-publish","format-standard","hentry","category-greedy","category-string","tag-greedy","tag-hard","tag-palindrome","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5393","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=5393"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5393\/revisions"}],"predecessor-version":[{"id":5397,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5393\/revisions\/5397"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}