{"id":8056,"date":"2021-01-30T22:04:31","date_gmt":"2021-01-31T06:04:31","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8056"},"modified":"2021-01-31T09:33:12","modified_gmt":"2021-01-31T17:33:12","slug":"leetcode-1745-palindrome-partitioning-iv","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1745-palindrome-partitioning-iv\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1745. Palindrome Partitioning IV"},"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 1745. Palindrome Partitioning IV - \u5237\u9898\u627e\u5de5\u4f5c EP381\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/kpswaxqyqEM?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>Given a string\u00a0<code>s<\/code>, return\u00a0<code>true<\/code>\u00a0<em>if it is possible to split the string<\/em>\u00a0<code>s<\/code>\u00a0<em>into three\u00a0<strong>non-empty<\/strong>\u00a0palindromic substrings. Otherwise, return\u00a0<\/em><code>false<\/code>.\u200b\u200b\u200b\u200b\u200b<\/p>\n\n\n\n<p>A string is said to be palindrome if it the same string when reversed.<\/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> s = \"abcbdd\"\n<strong>Output:<\/strong> true\n<strong>Explanation: <\/strong>\"abcbdd\" = \"a\" + \"bcb\" + \"dd\", and all three substrings are palindromes.\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 = \"bcbddxy\"\n<strong>Output:<\/strong> false\n<strong>Explanation: <\/strong>s cannot be split into 3 palindromes.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= s.length &lt;= 2000<\/code><\/li><li><code>s<\/code>\u200b\u200b\u200b\u200b\u200b\u200b consists only 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\/01\/1745-ep381-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1745-ep381-1.png\" alt=\"\" class=\"wp-image-8059\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1745-ep381-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1745-ep381-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1745-ep381-1-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\/01\/1745-ep381-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1745-ep381-2.png\" alt=\"\" class=\"wp-image-8060\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1745-ep381-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1745-ep381-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2021\/01\/1745-ep381-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/a><\/figure>\n\n\n\n<p>dp[i][j] := whether s[i]~s[j] is a palindrome.<\/p>\n\n\n\n<p>dp[i][j] = s[i] == s[j] and dp[i+1][j-1]<\/p>\n\n\n\n<p>ans = any(dp[0][i-1] and dp[i][j] and dp[j][n-1]) for j in range(i, n &#8211; 1) for i in range(1, n)<\/p>\n\n\n\n<p>Time complexity: O(n^2)<br>Space complexity: O(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  bool checkPartitioning(string s) {\n    const int n = s.length();\n    vector<vector<int>> dp(n, vector<int>(n, 1));    \n    for (int l = 2; l <= n; ++l)\n      for (int i = 0, j = i + l - 1; j < n; ++i, ++j)\n        dp[i][j] = (s[i] == s[j]) &#038;&#038; dp[i + 1][j - 1];\n    for (int i = 1; i < n; ++i)\n      for (int j = i; j + 1 < n; ++j)\n        if (dp[0][i - 1] &#038;&#038; dp[i][j] &#038;&#038; dp[j + 1][n - 1])\n          return true;\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a string\u00a0s, return\u00a0true\u00a0if it is possible to split the string\u00a0s\u00a0into three\u00a0non-empty\u00a0palindromic substrings. Otherwise, return\u00a0false.\u200b\u200b\u200b\u200b\u200b A string is said to be palindrome if it 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":[46],"tags":[18],"class_list":["post-8056","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8056","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=8056"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8056\/revisions"}],"predecessor-version":[{"id":8061,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8056\/revisions\/8061"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}