logo

Top-Down Approach for Palindrome Partitioning Problem

3 Pages984 Words331 Views
   

Added on  2023-05-30

About This Document

This article explains the Palindrome Partitioning Problem and how to solve it using Top-Down Approach with Java Codes and References. It covers the pseudocode, recursion, and memoization techniques used to solve the problem. The program uses Dynamic Programming solution storing the correct answers to subproblems in two arrays namely pal[][] and count[][], and keeps reusing the values stored.

Top-Down Approach for Palindrome Partitioning Problem

   Added on 2023-05-30

ShareRelated Documents
Top-down approach; pseudocode with java codes and references
Pseudocode and description of the problem using top-down approach ,recursion and memoization.
Given input as String strin;
// i is the beginning index passed as i=0 while j marks the end index j=n-1;
Using recurrence, search for the partions and then store them in an array while making Boolean array
true; Below is an optimal substructure of the program
//Base cases
If the length of string ==1
BestPal(strin,i,j)=1;
If the String is a palindrome then
Bestpal(strin,i,j)==1;
If both of them are false,then
Bespal(strin,i,j) is computed through recurrence implementing the following formula
Bestpal(strin,i,j)=Minimum{Bestpal(strin,i,k) + k + 1 + Bestpal(strin,i,k +1,j)}
Where the k changes ranging from (i) to j-1;
Note if a more optimized solution is needed ,of which your assignments requires then the first
bottom up approach is advised due to its complexity of O(n2) unlike this which is O(n3);
Below is a java program to implement the pseudocode using Dynamic Programming solution storing the
correct answers to subproblems in two arrays namely pal[][] and count[][], and keeps reusing the
values stored:
import java.io.*;
import java.util.*;
public class findpalindromes
{
//java program to find the minimum cuts in palindromic partioning using top-down
approach and memoization technique
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
String s=in.next();
//String s=“madamanna”;
System.out.println("The value of Best-palindrome of string s is" + bestpal(s));
}
//this method Returns the integer representing number
// of partitioning the given string to ensure each
// part is a palindrome
static int bestpal(String strin)
{
//let length to be the length of the given string
int length=strin.length();
/* We need to create two arrays to store , compute and check if values therein are
computed and if not then we create one
count[i][j]=this represents the min no of cuts required for palindrome partitioning
Top-Down Approach for Palindrome Partitioning Problem_1

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
Implementation of Palindrome check from the words of the file
|7
|531
|1

Please use the file provided for coding..
|2
|959
|64

Java Programming Assignment: Comma-Separated Integer Inputs and Exception Handling
|8
|937
|481

Source codes for Maximum Flow Algorithm
|9
|913
|366

Design Documentation Program Algorithm
|15
|2080
|86