Repository: TheAlgorithms/Java
Pull request #5402 (2024-08-26 13:21:51)

test: `LongestPalindromicSubstring`
Added test LongestPalindromicSubstringTest

<!--
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/5402?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 48.13%. Comparing base [(`b70f077`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/b70f077343a4629cf163400f1d64993c6ab2bbf7?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`5e1d89d`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/5e1d89d8559421d1edb4ee0e87305febac15b2d3?dropdown=coverage&el=desc&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    #5402      +/-   ##
============================================
+ Coverage     48.02%   48.13%   +0.11%     
- Complexity     2962     2972      +10     
============================================
  Files           524      524              
  Lines         15275    15265      -10     
  Branches       2903     2902       -1     
============================================
+ Hits           7336     7348      +12     
+ Misses         7637     7614      -23     
- Partials        302      303       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5402?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/strings/LongestPalindromicSubstring.java b/src/main/java/com/thealgorithms/strings/LongestPalindromicSubstring.java
index fa917113..ca500357 100644
--- a/src/main/java/com/thealgorithms/strings/LongestPalindromicSubstring.java
+++ b/src/main/java/com/thealgorithms/strings/LongestPalindromicSubstring.java
@@ -1,44 +1,31 @@
 package com.thealgorithms.strings;
 
-// Longest Palindromic Substring
-import java.util.Scanner;
-
 final class LongestPalindromicSubstring {
     private LongestPalindromicSubstring() {
     }
 
-    public static void main(String[] args) {
-        Solution s = new Solution();
-        String str = "";
-        Scanner sc = new Scanner(System.in);
-        System.out.print("Enter the string: ");
-        str = sc.nextLine();
-        System.out.println("Longest substring is : " + s.longestPalindrome(str));
-        sc.close();
-    }
-}
-
-class Solution {
-
-    public String longestPalindrome(String s) {
-        if (s == null || s.length() == 0) {
+    /**
+     * Finds the longest palindromic substring in the given string.
+     *
+     * @param s the input string
+     * @return the longest palindromic substring
+     */
+    public static String longestPalindrome(String s) {
+        if (s == null || s.isEmpty()) {
             return "";
         }
-        int n = s.length();
         String maxStr = "";
-        for (int i = 0; i < n; ++i) {
-            for (int j = i; j < n; ++j) {
-                if (isValid(s, i, j)) {
-                    if (j - i + 1 > maxStr.length()) { // update maxStr
-                        maxStr = s.substring(i, j + 1);
-                    }
+        for (int i = 0; i < s.length(); ++i) {
+            for (int j = i; j < s.length(); ++j) {
+                if (isValid(s, i, j) && (j - i + 1 > maxStr.length())) {
+                    maxStr = s.substring(i, j + 1);
                 }
             }
         }
         return maxStr;
     }
 
-    private boolean isValid(String s, int lo, int hi) {
+    private static boolean isValid(String s, int lo, int hi) {
         int n = hi - lo + 1;
         for (int i = 0; i < n / 2; ++i) {
             if (s.charAt(lo + i) != s.charAt(hi - i)) {


Test patch:
diff --git a/src/test/java/com/thealgorithms/strings/LongestPalindromicSubstringTest.java b/src/test/java/com/thealgorithms/strings/LongestPalindromicSubstringTest.java
new file mode 100644
index 00000000..aa13c0f4
--- /dev/null
+++ b/src/test/java/com/thealgorithms/strings/LongestPalindromicSubstringTest.java
@@ -0,0 +1,21 @@
+package com.thealgorithms.strings;
+
+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;
+
+class LongestPalindromicSubstringTest {
+
+    @ParameterizedTest
+    @MethodSource("provideTestCasesForLongestPalindrome")
+    void testLongestPalindrome(String input, String expected) {
+        assertEquals(expected, LongestPalindromicSubstring.longestPalindrome(input));
+    }
+
+    private static Stream<Arguments> provideTestCasesForLongestPalindrome() {
+        return Stream.of(Arguments.of("babad", "bab"), Arguments.of("cbbd", "bb"), Arguments.of("a", "a"), Arguments.of("", ""), Arguments.of("abc", "a"), Arguments.of(null, ""), Arguments.of("aaaaa", "aaaaa"));
+    }
+}