×

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed.

Palindrome

If a string remains unchanged whether it is read from the left to the right or the right to the left, it is considered to be a palindrome.

Palindrome Partitioning

When a string is partitioned into palindromes, it is done so that each substring created from the original string is really a palindrome in and of itself. In the Java palindrome partitioning issue, we provide the lowest number of cuts needed to convert each of the provided string's substrings into a palindrome. Let's break it down with the aid of some examples.

A matrix chain multiplication issue variant is this one. The string is simply returned as 0, if it is a palindrome. If not, we attempt to make cuts in every location that is feasible, recursively determine the cost for each cut, and then return the minimal value, similar to a Matrix Chain Multiplication issue.

Take str as the input string, and let minPalPartion() return the function that requires the fewest cuts to partition a palindrome.

Example:

Str = “madam”

At most cuts needed to the above string are 0. Since the string is a palindrome.

Str = “kebab”

At most cuts needed to the above string are 2. Since the string is not a palindrome. We may divide the provided text into 3 substrings using the 2 cuts, each of which is a palindrome.

There are many approaches in which a palindrome partitioning can be achieved. They are

  • By recursion
  • By Dynamic programming
  • By memorization

By Recursion

Let us understand the partitioning of palindrome by recursion method

File name: Recurpalin.java

// Java program for implementation of palindrome partitioning by recursion method
public class Recurpalin
{
static boolean isPalindrome(String string, int i, int j)
{
while(i < j)
{
if(string.charAt(i) != string.charAt(j))
return false;
i++;
j--;
}
return true;
}
static int minPalPartion(String string, int i, int j)
{
if( i >= j || isPalindrome(string, i, j) )
return 0;
int ans = Integer.MAX_VALUE, count;
for(int k = i; k < j; k++)
{
count = minPalPartion(string, i, k) +
minPalPartion(string, k + 1, j) + 1;
ans = Math.min(ans, count);
}
return ans;
}
public static void main(String args[])
{
String str = "kebab";
System.out.println("At most cuts needed in "
+ "Palindrome Partitioning is " + minPalPartion(str, 0, str.length() - 1));
}
}

Output

At most cuts needed in Palindrome Partitioning is 2

By Dynamic Programming

The dynamic programming solution is shown below. It seems to use the calculated values and stores the responses to the subproblems in two arrays. Let u sunderstand it through the program flow.

Let us understand the program flow for achieving the palindrome partitioning. The approach that discussed below implements the dynamic programming. It is not really a program but the function body of how the methodology is implemented for partitioning the palindrome. It was proved that this approach isn’t the perfect approach. We are using for loops that are nested to a threshold of three to locate the solution. This consumes a lot of time for execution of program. So an optimized approach is also discovered.

class Dynampalin {


public static int minCut(String a)
{
int[] cut = new int[a.length()];
boolean[][] palindrome = new boolean[a.length()][a.length()];


for (int i = 0; i < a.length(); i++) {
int minCut = i;
for (int j = 0; j <= i; j++) {
if (a.charAt(i) == a.charAt(j) && (i - j < 2 || palindrome[j + 1][i - 1])) {
palindrome[j][i] = true;
minCut = Math.min(minCut, j == 0 ? 0 : (cut[j - 1] + 1));
}
}
cut[i] = minCut;
}


return cut[a.length() - 1];
}
}

Now let us go through the program that runs with a optimized approach. The interviewer will typically reject the above solution, but they can also ask that you enhance it. The offered solution never possesses the third level of for-loop nesting. The logic after optimization has been found that the time consumption or the time complexity is comparatively less than the previously discussed function. Hence this method is more often used for the purpose of palindrome partitioning. 

File name: Dynampalin1.java

//Java program for implementation of palindrome partitioning by dynamic programming    //method
public class Dynampalin1{
static int minPalPartion(String str)
{
int n = str.length();
int[] C = new int[n];
boolean[][] P = new boolean[n][n];


int i, j, k, L; 
for (i = 0; i < n; i++) {
P[i][i] = true;
}
/* Substring length L is. Consider all substrings with lengths ranging from 2 to n as you build the solution from the bottom up.*/
for (L = 2; L <= n; L++) {
// Different starting indexes should be established for a substring of\
                                       //length L.
for (i = 0; i < n - L + 1; i++) {
j = i + L - 1; // Set ending index
// We just need to compare two characters if L is 2. 
                                                      //Otherwise,two corner characters must be checked.
if (L == 2)
P[i][j] = (str.charAt(i) == str.charAt(j));
else
P[i][j] = (str.charAt(i) == str.charAt(j)) && P[i + 1][j - 1];
}
}
for (i = 0; i < n; i++) {
if (P[0][i] == true)
C[i] = 0;
else {
C[i] = Integer.MAX_VALUE;
for (j = 0; j < i; j++) {
if (P[j + 1][i] == true && 1 + C[j] < C[i])
C[i] = 1 + C[j];
}
}
}
// Obtain the string's minimum cut value.
return C[n - 1];
}
public static void main(String args[])
{
String str = "kebab";
System.out.println("At most cuts needed in "
+ "Palindrome Partitioning"
+ " is " + minPalPartion(str));
}
}

Output

At most cuts needed in Palindrome Partitioning is 2

By Memorization

The fundamental concept is to cache the sporadic results computed by recursive functions. These outcomes can be placed in a hashmap or unordered map.

The starting and ending indexes of the text will be used to construct the keys for such hashmap, so ["start index".append("end index")] would be the key.

Let us understand the below program for understanding the above approach.

File name: Memopalin.java

import java.io.*;
import java.util.*;


class Memopalin {
// Leveraging memorization to overcome the partition problems.


// method to determine whether an input string is a palindrome
static boolean ispalindrome(String input, int start, int end)
{
// Using the two-pointer method to verify palindrome
while (start < end) {
if (input.charAt(start) != input.charAt(end))
return false;
start++;
end--;
}
return true;
}


// method to retrieve keys for hashmap
static String convert(int a, int b)
{
return String.valueOf(a) + "" + String.valueOf(b);
}


/* Returns the smallest number of cuts necessary to divide a string into sections where each one is a palindrome.*/
static int minpalparti_memo(String input, int i, int j, HashMap<String, Integer> memo)
{
if (i > j)
return 0;
// The input string's key
String ij = convert(i, j);


/* If the number of partitions for the string "ij" has already been determined, then use the Hashmap to return the calculated result.*/
if (memo.containsKey(ij)) {
return memo.get(ij);
}
// A palindrome exists for every String of size 1.
if (i == j) {
memo.put(ij, 0);
return 0;
}
if (ispalindrome(input, i, j)) {
memo.put(ij, 0);
return 0;
}
int minimum = Integer.MAX_VALUE;
// Make cuts from I to j in every direction that is possible.
for (int k = i; k < j; k++) {
int left_min = Integer.MAX_VALUE;
int right_min = Integer.MAX_VALUE;
String left = convert(i, k);
String right = convert(k + 1, j);


// if the left cut is already clear
if (memo.containsKey(left)) {
left_min = memo.get(left);
}
// if the appropriate cut is already discovered
if (memo.containsKey(right)) {
right_min = memo.get(right);
}


// the left as well as right strings are calculated recursively
if (left_min == Integer.MAX_VALUE)
left_min = minpalparti_memo(input, i, k, memo);
if (right_min == Integer.MAX_VALUE)
right_min = minpalparti_memo(input, k + 1, j, memo);


// minimising all potential cuts to k
minimum = Math.min(minimum, left_min + 1 + right_min);
}
memo.put(ij, minimum);
// For the entire string, return the minimum cut value.
return memo.get(ij);
}

public static void main(String args[])
{
String input = "kebab";
HashMap<String, Integer> memo = new HashMap<>();
System.out.println(minpalparti_memo(input, 0, input.length() - 1, memo));
}
}

Output

At most cuts required in palindrome partitioning are 2

Related Topics

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

How Many Ways to Create an Object in Java?

As you know, a class is a blueprint for an object, and you can create objects from it. There are several ways to create objects of classes in Java. This...

6 minutes read.

Star Program in Java

By solving the patterns, we can develop our coding skills and logical thinking. Mostly each pattern program uses two or more loops. Loops' number depends on the complexity of logic....

3 minutes read.

Java Read File

Java provides its users with many ways of reading a file. To read a text file, you can use a FileReader, BufferedReader, or Scanner. Each utility offers something special for...

6 minutes read.

Hamming Code in Java

In a computer network, hamming code is a unique set of error-correction codes. It is mostly utilised in computer graphics for mistake detection and correction during data transmission from sender...

8 minutes read.

Java Program to find the smallest element in a tree

The variable min is used to store the data of the root, which is initially defined. The smallest node in the left subtree is then located by moving through the...

3 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Java String Reader

StreamReaderClass: This class is present in the java.io package. It is a character stream in which string act as a source.This method provides to read characters from a string. Character Stream: This class...

3 minutes read.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

Java Double Keyword

In java primitive data types, we have two different types of data types which are Boolean and floating-point data types.In floating data type again we have four types which are...

3 minutes read.

Java Generics Questions

Introduction We'll walk through a few real-world examples of interview questions and responses for Java generics in this article. Java 5 saw the debut of the fundamental idea of generics. Due to...

9 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

How to Return Value from Lambda Expression Java?

What is Lambda Expression in Java? In Java 8, Lambda Expressions were introduced.A lambda expression is a brief section of code that accepts input and outputs a value. Similar to methods,...

4 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.