Skip to content

Commit 7d1847f

Browse files
alxkmalxkm
and
alxkm
authored
refactor: PalindromicPartitioning (#5419)
refactor: PalindromicPartitioning Co-authored-by: alxkm <[email protected]>
1 parent 0733075 commit 7d1847f

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioning.java

+19-26
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
11
package com.thealgorithms.dynamicprogramming;
22

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

24-
public static int minimalpartitions(String word) {
28+
public static int minimalPartitions(String word) {
2529
int len = word.length();
2630
/* We Make two arrays to create a bottom-up solution.
2731
minCuts[i] = Minimum number of cuts needed for palindrome partitioning of substring
@@ -76,15 +80,4 @@ public static int minimalpartitions(String word) {
7680
// string. i.e., str[0..n-1]
7781
return minCuts[len - 1];
7882
}
79-
80-
public static void main(String[] args) {
81-
Scanner input = new Scanner(System.in);
82-
String word;
83-
System.out.println("Enter the First String");
84-
word = input.nextLine();
85-
// ans stores the final minimal cut count needed for partitioning
86-
int ans = minimalpartitions(word);
87-
System.out.println("The minimum cuts needed to partition \"" + word + "\" into palindromes is " + ans);
88-
input.close();
89-
}
9083
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.Arguments;
8+
import org.junit.jupiter.params.provider.MethodSource;
9+
10+
public class PalindromicPartitioningTest {
11+
12+
private static Stream<Arguments> provideTestCases() {
13+
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));
14+
}
15+
16+
@ParameterizedTest
17+
@MethodSource("provideTestCases")
18+
public void testMinimalPartitions(String input, int expected) {
19+
assertEquals(expected, PalindromicPartitioning.minimalPartitions(input));
20+
}
21+
}

0 commit comments

Comments
 (0)