{"id":6046,"date":"2020-01-04T21:57:25","date_gmt":"2020-01-05T05:57:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6046"},"modified":"2020-01-04T23:56:55","modified_gmt":"2020-01-05T07:56:55","slug":"leetcode-1312-minimum-insertion-steps-to-make-a-string-palindrome","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1312-minimum-insertion-steps-to-make-a-string-palindrome\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1312. Minimum Insertion Steps to Make a String Palindrome"},"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 1312. Minimum Insertion Steps to Make a String Palindrome - \u5237\u9898\u627e\u5de5\u4f5c EP293\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/XNWz9xbX8F0?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;<code>s<\/code>. In one step you can insert any character at any index of the string.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum number of steps<\/em>&nbsp;to make&nbsp;<code>s<\/code>&nbsp;palindrome.<\/p>\n\n\n\n<p>A&nbsp;<strong>Palindrome String<\/strong>&nbsp;is one that reads the same backward as well as forward.<\/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 = \"zzazz\"\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The string \"zzazz\" is already palindrome we don't need any insertions.\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 = \"mbadm\"\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> String can be \"mbdadbm\" or \"mdbabdm\".\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> s = \"leetcode\"\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> Inserting 5 characters the string becomes \"leetcodocteel\".\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> s = \"g\"\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"no\"\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 500<\/code><\/li><li>All characters of&nbsp;<code>s<\/code>&nbsp;are lower case 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\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/01\/1312-ep293.png\" alt=\"\" class=\"wp-image-6050\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/01\/1312-ep293.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/01\/1312-ep293-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/01\/1312-ep293-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>dp[i][j] := min chars to insert<br>dp[j][j] =  dp[i-1][j+1] if s[i] == s[j]  else min(dp[i+1][j] , dp[i][j-1]) + 1<br>base case: dp[i][i] = 0<br>ans: dp[0][n-1]<\/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 minInsertions(string s) {\n    const int n = s.length();\n    vector<vector<int>> dp(n, vector<int>(n));\n    \n    for (int l = 2; l <= n; ++l)\n      for (int i = 0, j = l - 1; j < n; ++i, ++j)        \n        dp[i][j] = s[i] == s[j] ? dp[i + 1][j - 1] : min(dp[i + 1][j], dp[i][j - 1]) + 1;                        \n    return dp[0][n - 1];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s. In one step you can insert any character at any index of the string. Return&nbsp;the minimum number of steps&nbsp;to make&nbsp;s&nbsp;palindrome. A&nbsp;Palindrome String&nbsp;is&#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-6046","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\/6046","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=6046"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6046\/revisions"}],"predecessor-version":[{"id":6051,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6046\/revisions\/6051"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}