{"id":26291,"date":"2017-10-26T21:48:07","date_gmt":"2017-10-26T16:18:07","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26291"},"modified":"2017-10-26T21:48:07","modified_gmt":"2017-10-26T16:18:07","slug":"palindrome-partitioning","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/palindrome-partitioning\/","title":{"rendered":"Palindrome Partitioning"},"content":{"rendered":"<p>Given a string, a partitioning of the string is a <em>palindrome partitioning<\/em> if every substring of the partition is a palindrome. <span id=\"more-20293\"><\/span>For example, \u201caba|b|bbabb|a|b|aba\u201d is a palindrome partitioning of \u201cababbbabbababa\u201d. Determine the fewest cuts needed for palindrome partitioning of a given string. For example, minimum 3 cuts are needed for \u201cababbbabbababa\u201d. The three cuts are \u201ca|babbbab|b|ababa\u201d. If a string is palindrome, then minimum 0 cuts are needed. If a string of length n containing all different characters, then minimum n-1 cuts are needed.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26388\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Palindrome-Partitioning.png\" alt=\"Palindrome Partitioning\" width=\"770\" height=\"135\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Palindrome-Partitioning.png 770w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Palindrome-Partitioning-300x53.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Palindrome-Partitioning-768x135.png 768w\" sizes=\"(max-width: 770px) 100vw, 770px\" \/><\/p>\n<p><strong>Solution<\/strong><br \/>\nThis problem is a variation of Matrix Chain Multiplication problem. If the string is palindrome, then we simply return 0. Else, like the Matrix Chain Multiplication problem, we try making cuts at all possible places, recursively calculate the cost for each cut and return the minimum value.<\/p>\n<p>Let the given string be str and minPalPartion() be the function that returns the fewest cuts needed for palindrome partitioning. following is the optimal substructure property.<\/p>\n<pre>\/\/ i is the starting index and j is the ending index. i must be passed as 0 and j as n-1\r\nminPalPartion(str, i, j) = 0 if i == j. \/\/ When string is of length 1.\r\nminPalPartion(str, i, j) = 0 if str[i..j] is palindrome.\r\n\r\n\/\/ If none of the above conditions is true, then minPalPartion(str, i, j) can be \r\n\/\/ calculated recursively using the following formula.\r\nminPalPartion(str, i, j) = Min { minPalPartion(str, i, k) + 1 +\r\n                                 minPalPartion(str, k+1, j) } \r\n                           where k varies from i to j-1<\/pre>\n<p>Following is Dynamic Programming solution. It stores the solutions to subproblems in two arrays P[][] and C[][], and reuses the calculated values.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Dynamic Programming Solution for Palindrome Partitioning Problem<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/>#include &lt;limits.h&gt;<br\/>  <br\/>\/\/ A utility function to get minimum of two integers<br\/>int min (int a, int b) { return (a &lt; b)? a : b; }<br\/>  <br\/>\/\/ Returns the minimum number of cuts needed to partition a string<br\/>\/\/ such that every part is a palindrome<br\/>int minPalPartion(char *str)<br\/>{<br\/>    \/\/ Get the length of the string<br\/>    int n = strlen(str);<br\/>  <br\/>    \/* Create two arrays to build the solution in bottom up manner<br\/>       C[i][j] = Minimum number of cuts needed for palindrome partitioning<br\/>                 of substring str[i..j]<br\/>       P[i][j] = true if substring str[i..j] is palindrome, else false<br\/>       Note that C[i][j] is 0 if P[i][j] is true *\/<br\/>    int C[n][n];<br\/>    bool P[n][n];<br\/>  <br\/>    int i, j, k, L; \/\/ different looping variables<br\/>  <br\/>    \/\/ Every substring of length 1 is a palindrome<br\/>    for (i=0; i&lt;n; i++)<br\/>    {<br\/>        P[i][i] = true;<br\/>        C[i][i] = 0;<br\/>    }<br\/>  <br\/>    \/* L is substring length. Build the solution in bottom up manner by<br\/>       considering all substrings of length starting from 2 to n.<br\/>       The loop structure is same as Matrx Chain Multiplication problem (<br\/>       See http:\/\/www.geeksforgeeks.org\/archives\/15553 )*\/<br\/>    for (L=2; L&lt;=n; L++)<br\/>    {<br\/>        \/\/ For substring of length L, set different possible starting indexes<br\/>        for (i=0; i&lt;n-L+1; i++)<br\/>        {<br\/>            j = i+L-1; \/\/ Set ending index<br\/>  <br\/>            \/\/ If L is 2, then we just need to compare two characters. Else<br\/>            \/\/ need to check two corner characters and value of P[i+1][j-1]<br\/>            if (L == 2)<br\/>                P[i][j] = (str[i] == str[j]);<br\/>            else<br\/>                P[i][j] = (str[i] == str[j]) &amp;&amp; P[i+1][j-1];<br\/>  <br\/>            \/\/ IF str[i..j] is palindrome, then C[i][j] is 0<br\/>            if (P[i][j] == true)<br\/>                C[i][j] = 0;<br\/>            else<br\/>            {<br\/>                \/\/ Make a cut at every possible localtion starting from i to j,<br\/>                \/\/ and get the minimum cost cut.<br\/>                C[i][j] = INT_MAX;<br\/>                for (k=i; k&lt;=j-1; k++)<br\/>                    C[i][j] = min (C[i][j], C[i][k] + C[k+1][j]+1);<br\/>            }<br\/>        }<br\/>    }<br\/>  <br\/>    \/\/ Return the min cut value for complete string. i.e., str[0..n-1]<br\/>    return C[0][n-1];<br\/>}<br\/>  <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>   char str[] = &quot;ababbbabbababa&quot;;<br\/>   printf(&quot;Min cuts needed for Palindrome Partitioning is %d&quot;,<br\/>           minPalPartion(str));<br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output :<\/strong><\/p>\n<pre>Min cuts needed for Palindrome Partitioning is 3<\/pre>\n<p>Time Complexity: O(n<sup>3<\/sup>)<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>An optimization to above approach<\/strong><br \/>\nIn above approach, we can calculating minimum cut while finding all palindromic substring. If we finding all palindromic substring 1<sup>st<\/sup> and then we calculate minimum cut, time complexity will reduce to O(n<sup>2<\/sup>).<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ Dynamic Programming Solution for Palindrome Partitioning Problem<br\/>#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/>#include &lt;limits.h&gt;<br\/>  <br\/>\/\/ A utility function to get minimum of two integers<br\/>int min (int a, int b) { return (a &lt; b)? a : b; }<br\/>  <br\/>\/\/ Returns the minimum number of cuts needed to partition a string<br\/>\/\/ such that every part is a palindrome<br\/>int minPalPartion(char *str)<br\/>{<br\/>    \/\/ Get the length of the string<br\/>    int n = strlen(str);<br\/>  <br\/>    \/* Create two arrays to build the solution in bottom up manner<br\/>       C[i] = Minimum number of cuts needed for palindrome partitioning<br\/>                 of substring str[0..i]<br\/>       P[i][j] = true if substring str[i..j] is palindrome, else false<br\/>       Note that C[i] is 0 if P[0][i] is true *\/<br\/>    int C[n];<br\/>    bool P[n][n];<br\/>  <br\/>    int i, j, k, L; \/\/ different looping variables<br\/>  <br\/>    \/\/ Every substring of length 1 is a palindrome<br\/>    for (i=0; i&lt;n; i++)<br\/>    {<br\/>        P[i][i] = true;<br\/>    }<br\/>  <br\/>    \/* L is substring length. Build the solution in bottom up manner by<br\/>       considering all substrings of length starting from 2 to n. *\/<br\/>    for (L=2; L&lt;=n; L++)<br\/>    {<br\/>        \/\/ For substring of length L, set different possible starting indexes<br\/>        for (i=0; i&lt;n-L+1; i++)<br\/>        {<br\/>            j = i+L-1; \/\/ Set ending index<br\/>  <br\/>            \/\/ If L is 2, then we just need to compare two characters. Else<br\/>            \/\/ need to check two corner characters and value of P[i+1][j-1]<br\/>            if (L == 2)<br\/>                P[i][j] = (str[i] == str[j]);<br\/>            else<br\/>                P[i][j] = (str[i] == str[j]) &amp;&amp; P[i+1][j-1];<br\/>        }<br\/>    }<br\/> <br\/>    for (i=0; i&lt;n; i++)<br\/>    {<br\/>        if (P[0][i] == true)<br\/>            C[i] = 0;<br\/>        else<br\/>        {<br\/>            C[i] = INT_MAX;<br\/>            for(j=0;j&lt;i;j++)<br\/>            {<br\/>                if(P[j+1][i] == true &amp;&amp; 1+C[j]&lt;C[i])<br\/>                    C[i]=1+C[j];<br\/>            }<br\/>        }<br\/>    }<br\/>  <br\/>    \/\/ Return the min cut value for complete string. i.e., str[0..n-1]<br\/>    return C[n-1];<br\/>}<br\/>  <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>   char str[] = &quot;ababbbabbababa&quot;;<br\/>   printf(&quot;Min cuts needed for Palindrome Partitioning is %d&quot;,<br\/>           minPalPartion(str));<br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output :<\/strong><\/p>\n<pre>Min cuts needed for Palindrome Partitioning is 3<\/pre>\n<p>Time Complexity: O(n<sup>2<\/sup>)<\/p>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Palindrome Partitioning &#8211; Dynamic Programming a partitioning of the string is a palindrome partitioning if every substring of the partition is a palindrome.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,70145,83623],"tags":[72847,72842,72845,70483,72848,72840,72846,72994,72843,72992,72854,72844,72850,72839,78581,78577,78583,78585,78578,78582,78580,78584,78579,78576,72852,72855,78586,72853,72851],"class_list":["post-26291","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-dynamic-programming","category-palindrome-partitioning","tag-concept-of-dynamic-programming","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-code-generation-algorithm","tag-dynamic-programming-definition","tag-dynamic-programming-in-data-structure","tag-dynamic-programming-in-python","tag-dynamic-programming-problems","tag-dynamic-programming-python","tag-dynamic-programming-set-1","tag-dynamic-programming-software","tag-explain-dynamic-programming","tag-how-to-solve-dynamic-programming-problems","tag-minimum-cut-palindrome","tag-palindrome-partition-dynamic-programming","tag-palindrome-partitioning-backtracking","tag-palindrome-partitioning-concept","tag-palindrome-partitioning-dynamic-programming","tag-palindrome-partitioning-ii","tag-palindrome-partitioning-in-c-code","tag-palindrome-partitioning-in-dynamic-programming","tag-palindrome-partitioning-java","tag-print-all-palindromic-partitions-of-a-string","tag-problems-on-dynamic-programming","tag-simple-dynamic-programming-example","tag-split-string-into-palindromes","tag-types-of-dynamic-programming","tag-youtube-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26291"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26291\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}