diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioning.java b/src/main/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioning.java index 01fa1d19..a323a9a0 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioning.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioning.java @@ -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. * - * @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) + *

+ * 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. + *

+ * + *

+ * Example: + *

+ *

+ * + * @see Palindrome Partitioning II + * @see Palindrome Partitioning (GeeksforGeeks) */ 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 @@ -76,15 +80,4 @@ public final class PalindromicPartitioning { // 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(); - } } diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioningTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioningTest.java new file mode 100644 index 00000000..be503359 --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioningTest.java @@ -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 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)); + } +}