{"id":5929,"date":"2019-12-07T13:01:04","date_gmt":"2019-12-07T21:01:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5929"},"modified":"2019-12-07T13:26:45","modified_gmt":"2019-12-07T21:26:45","slug":"leetcode-132-palindrome-partitioning-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-132-palindrome-partitioning-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 132. Palindrome Partitioning II"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 132. Palindrome Partitioning II - \u5237\u9898\u627e\u5de5\u4f5c EP281\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/kTCymFbU2ok?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&nbsp;<em>s<\/em>, partition&nbsp;<em>s<\/em>&nbsp;such that every substring of the partition is a palindrome.<\/p>\n\n\n\n<p>Return the minimum cuts needed for a palindrome partitioning of&nbsp;<em>s<\/em>.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong>&nbsp;\"aab\"\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The palindrome partitioning [\"aa\",\"b\"] could be produced using 1 cut.<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/132-ep281-1.png\" alt=\"\" class=\"wp-image-5930\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/132-ep281-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/132-ep281-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/132-ep281-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/132-ep281-2.png\" alt=\"\" class=\"wp-image-5931\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/132-ep281-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/132-ep281-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/132-ep281-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>dp[i] := min cuts of s[0~i]<br>dp[i] = min{dp[j] + 1} if s[j+1~i] is a palindrome.<\/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  int minCut(string s) {\n    const int n = s.length();\n    \/\/ valid[i][j] = 1 if s[i~j] is palindrome, otherwise 0\n    vector<vector<int>> valid(n, vector<int>(n, 1));\n    \n    \/\/ dp[i] = min cuts of s[0~i] \n    vector<int> dp(n, n);\n    \n    for (int l = 2; l <= n; ++l)\n      for (int i = 0, j = i + l - 1; j < n; ++i, ++j)\n        valid[i][j] = s[i] == s[j] &#038;&#038; valid[i + 1][j - 1];\n    \n    for (int i = 0; i < n; ++i) {      \n      if (valid[0][i]) {\n        dp[i] = 0;\n        continue;\n      }\n      for (int j = 0; j < i; ++j)\n        if (valid[j + 1][i])\n          dp[i] = min(dp[i], dp[j] + 1);      \n    }\n    return dp[n - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>DP v2<\/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 minCut(string s) {\n    const int n = s.length();        \n    \/\/ dp[i] = min cuts of s[0~i] \n    vector<int> dp(n, n);    \n    for (int m = 0; m < n; ++m)      \n      for (int d = 0; d <= 1; ++d)\n        for (int i = m, j = m + d; i >= 0 && j < n &#038;&#038; s[i] == s[j]; --i, ++j)\n          dp[j] = min(dp[j], (i ? (dp[i - 1] + 1) : 0));    \n    return dp[n - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Related Problems<\/strong><\/h2>\n\n\n\n<p>131. <a href=\"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-131-palindrome-partitioning\/\">https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-131-palindrome-partitioning\/<\/a><br>1278. <a href=\"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1278-palindrome-partitioning-iii\/\">https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1278-palindrome-partitioning-iii\/<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s, partition&nbsp;s&nbsp;such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of&nbsp;s. Example: Input:&nbsp;&#8220;aab&#8221;&#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,217,95],"class_list":["post-5929","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-dp","tag-hard","tag-palindrome","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5929","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=5929"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5929\/revisions"}],"predecessor-version":[{"id":5934,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5929\/revisions\/5934"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}