Palindrome Partitioning\\n

In this algorithm, the input is a string, a partitioning of that string is palindrome partitioning when every substring of the partition is a palindrome.

In this algorithm, we have to find the minimum cuts are needed to palindrome partitioning the given string.

Input and Output

Input:
A string. Say “ababbbabbababa”
Output:
Minimum cut to partition as palindrome. Here 3 cuts are needed.
The palindromes are: a | babbbab | b | ababa

Algorithm

minPalPart(str)

Input: The given string.

Output: Minimum number of palindromic partitioning from the string.

Begin
   n := length of str
   define cut matrix and pal matrix each of order n x n

   for i := 0 to n, do
      pal[i, i] := true
      cut[i, i] := 0
   done

   for len in range 2 to n, do
      for i in range 0 to n – len, do
         j := i + len – 1
         if len = 2, then
            if str[i] = str[j]
               pal[i, j] := true
         else
            if str[i] = str[j] and pal[i+1, j-1] ≠ 0
               pal[i, j] := true

         if pal[i, j] is true, then
            cut[i, j] := 0
         else

            cut[i, j] := ∞
            for k in range i to j-1, do
               cut[i, j] := minimum of cut[i, j] and (cut[i, k]+ cut[k+1, j+1]+1)
            done
      done
   done
   return cut[0, n-1]
End

Example

#include 
using namespace std;

int min (int a, int b) {
   return (a 

Output

Min cuts for Palindrome Partitioning is: 3
Updated on: 2020-06-17T07:22:59+05:30

546 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements