Skip to content

refactor: PalindromicPartitioning #5419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
package com.thealgorithms.dynamicprogramming;

import java.util.Scanner;

/**
* @file @brief Implements [Palindrome
* Partitioning](https://leetcode.com/problems/palindrome-partitioning-ii/)
* algorithm, giving you the minimum number of partitions you need to make
* Provides functionality to solve the Palindrome Partitioning II problem, which involves finding
* the minimum number of partitions needed to divide a given string into palindromic substrings.
*
* <p>
* The problem is solved using dynamic programming. The approach involves checking all possible
* substrings and determining whether they are palindromes. The minimum number of cuts required
* for palindrome partitioning is computed in a bottom-up manner.
* </p>
*
* <p>
* Example:
* <ul>
* <li>Input: "nitik" => Output: 2 (Partitioning: "n | iti | k")</li>
* <li>Input: "ababbbabbababa" => Output: 3 (Partitioning: "aba | b | bbabb | ababa")</li>
* </ul>
* </p>
*
* @details palindrome partitioning uses dynamic programming and goes to all the
* possible partitions to find the minimum you are given a string and you need
* to give minimum number of partitions needed to divide it into a number of
* palindromes [Palindrome Partitioning]
* (https://www.geeksforgeeks.org/palindrome-partitioning-dp-17/) overall time
* complexity O(n^2) For example: example 1:- String : "nitik" Output : 2 => "n
* | iti | k" For example: example 2:- String : "ababbbabbababa" Output : 3 =>
* "aba | b | bbabb | ababa"
* @author [Syed] (https://github.com/roeticvampire)
* @see <a href="https://leetcode.com/problems/palindrome-partitioning-ii/">Palindrome Partitioning II</a>
* @see <a href="https://www.geeksforgeeks.org/palindrome-partitioning-dp-17/">Palindrome Partitioning (GeeksforGeeks)</a>
*/
public final class PalindromicPartitioning {
private PalindromicPartitioning() {
}

public static int minimalpartitions(String word) {
public static int minimalPartitions(String word) {
int len = word.length();
/* We Make two arrays to create a bottom-up solution.
minCuts[i] = Minimum number of cuts needed for palindrome partitioning of substring
Expand Down Expand Up @@ -76,15 +80,4 @@ public static int minimalpartitions(String word) {
// string. i.e., str[0..n-1]
return minCuts[len - 1];
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String word;
System.out.println("Enter the First String");
word = input.nextLine();
// ans stores the final minimal cut count needed for partitioning
int ans = minimalpartitions(word);
System.out.println("The minimum cuts needed to partition \"" + word + "\" into palindromes is " + ans);
input.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class PalindromicPartitioningTest {

private static Stream<Arguments> provideTestCases() {
return Stream.of(Arguments.of("a", 0), Arguments.of("aa", 0), Arguments.of("ab", 1), Arguments.of("ababbbabbababa", 3), Arguments.of("abcde", 4), Arguments.of("abacdcaba", 0));
}

@ParameterizedTest
@MethodSource("provideTestCases")
public void testMinimalPartitions(String input, int expected) {
assertEquals(expected, PalindromicPartitioning.minimalPartitions(input));
}
}