Repository: TheAlgorithms/Java
Pull request #5419 (2024-08-27 21:37:54)

refactor: `PalindromicPartitioning`
Class cleanup.
Refactoring javadocs.
Adding tests.

<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5419?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 49.48%. Comparing base [(`633b9d4`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/633b9d4112698029dfff18160791a0372db996b1?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`70277e8`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/70277e86627f1dc37bc0161a015042b385689171?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 1 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5419      +/-   ##
============================================
+ Coverage     49.36%   49.48%   +0.12%     
- Complexity     3067     3079      +12     
============================================
  Files           524      524              
  Lines         15204    15197       -7     
  Branches       2890     2890              
============================================
+ Hits           7505     7520      +15     
+ Misses         7388     7364      -24     
- Partials        311      313       +2     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5419?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
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.
+ *
+ * <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
@@ -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();
-    }
 }


Test patch:
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<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));
+    }
+}