Algorithms‎ > ‎Dynamic Programming‎ > ‎

Palindrome Partitioning

Given a string, a partitioning of the string is a palindrome partitioning if every substring of the partition is a palindrome. For example, “aba|b|bbabb|a|b|aba” is a palindrome partitioning of “ababbbabbababa”. Determine the fewest cuts needed for palindrome partitioning of a given string. For example, minimum 3 cuts are needed for “ababbbabbababa”. The three cuts are “a|babbbab|b|ababa”. 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.

Solution
This 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.

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.

```// i is the starting index and j is the ending index. i must be passed as 0 and j as n-1
minPalPartion(str, i, j) = 0 if i == j. // When string is of length 1.
minPalPartion(str, i, j) = 0 if str[i..j] is palindrome.

// If none of the above conditions is true, then minPalPartion(str, i, j) can be
// calculated recursively using the following formula.
minPalPartion(str, i, j) = Min { minPalPartion(str, i, k) + 1 +
minPalPartion(str, k+1, j) }
where k varies from i to j-1
```

Following is Dynamic Programming solution. It stores the solutions to subproblems in two arrays P[][] and C[][], and reuses the calculated values.

 `// Dynamic Programming Solution for Palindrome Partitioning Problem``#include ``#include ``#include ` `// A utility function to get minimum of two integers``int` `min (``int` `a, ``int` `b) { ``return` `(a < b)? a : b; }` `// Returns the minimum number of cuts needed to partition a string``// such that every part is a palindrome``int` `minPalPartion(``char` `*str)``{``    ``// Get the length of the string``    ``int` `n = ``strlen``(str);` `    ``/* Create two arrays to build the solution in bottom up manner``       ``C[i][j] = Minimum number of cuts needed for palindrome partitioning``                 ``of substring str[i..j]``       ``P[i][j] = true if substring str[i..j] is palindrome, else false``       ``Note that C[i][j] is 0 if P[i][j] is true */``    ``int` `C[n][n];``    ``bool` `P[n][n];` `    ``int` `i, j, k, L; ``// different looping variables` `    ``// Every substring of length 1 is a palindrome``    ``for` `(i=0; i

Output:

`Min cuts needed for Palindrome Partitioning is 3 `

Time Complexity: O(n^3)