Repository: TheAlgorithms/Java
Pull request #5420 (2024-08-27 21:42:56)

refactor: `LongestPalindromicSubstring`
Class cleanup.
Refactored javadocs.
Added 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/5420?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `0%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 49.47%. Comparing base [(`a9bc7c2`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/a9bc7c269d3146aa2d67d69f2922f1bb2b07b4c5?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`b23d36f`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b23d36f78ef0bbbae0809aa703c9111565336e9e?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).

| [Files](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5420?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...ynamicprogramming/LongestPalindromicSubstring.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5420?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fdynamicprogramming%2FLongestPalindromicSubstring.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9keW5hbWljcHJvZ3JhbW1pbmcvTG9uZ2VzdFBhbGluZHJvbWljU3Vic3RyaW5nLmphdmE=) | 0.00% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5420?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) |

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


```diff
@@             Coverage Diff              @@
##             master    #5420      +/-   ##
============================================
+ Coverage     49.34%   49.47%   +0.12%     
- Complexity     3067     3077      +10     
============================================
  Files           524      524              
  Lines         15204    15197       -7     
  Branches       2890     2890              
============================================
+ Hits           7503     7518      +15     
+ Misses         7389     7366      -23     
- Partials        312      313       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5420?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/LongestPalindromicSubstring.java b/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java
index c6ae5591..8a4ab2f5 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java
@@ -1,25 +1,18 @@
 package com.thealgorithms.dynamicprogramming;
 
-/*
- * Algorithm explanation https://leetcode.com/problems/longest-palindromic-substring/
+/**
+ * Class for finding the longest palindromic substring within a given string.
+ * <p>
+ * A palindromic substring is a sequence of characters that reads the same backward as forward.
+ * This class uses a dynamic programming approach to efficiently find the longest palindromic substring.
+ *
  */
 public final class LongestPalindromicSubstring {
     private LongestPalindromicSubstring() {
     }
 
-    public static void main(String[] args) {
-        String a = "babad";
-        String b = "cbbd";
-
-        String aLPS = lps(a);
-        String bLPS = lps(b);
-
-        System.out.println(a + " => " + aLPS);
-        System.out.println(b + " => " + bLPS);
-    }
-
-    private static String lps(String input) {
-        if (input == null || input.length() == 0) {
+    public static String lps(String input) {
+        if (input == null || input.isEmpty()) {
             return input;
         }
         boolean[][] arr = new boolean[input.length()][input.length()];


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java
new file mode 100644
index 00000000..278de7e1
--- /dev/null
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java
@@ -0,0 +1,22 @@
+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 LongestPalindromicSubstringTest {
+
+    private static Stream<Arguments> provideTestCases() {
+        return Stream.of(
+            Arguments.of("babad", "aba"), Arguments.of("cbbd", "bb"), Arguments.of("a", "a"), Arguments.of("x", "x"), Arguments.of("", ""), Arguments.of("aaaa", "aaaa"), Arguments.of("mm", "mm"), Arguments.of("level", "level"), Arguments.of("bananas", "anana"), Arguments.of("abacabad", "abacaba"));
+    }
+
+    @ParameterizedTest
+    @MethodSource("provideTestCases")
+    public void testLps(String input, String expected) {
+        assertEquals(expected, LongestPalindromicSubstring.lps(input));
+    }
+}