Skip to content

test: LongestPalindromicSubstring #5402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}