test: LongestPalindromicSubstring
(#5402)
* LongestPalindromicSubstring * fix Rule:CollapsibleIfStatements --------- Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
parent
7674a84f5b
commit
d810a1d4da
@ -1,44 +1,31 @@
|
|||||||
package com.thealgorithms.strings;
|
package com.thealgorithms.strings;
|
||||||
|
|
||||||
// Longest Palindromic Substring
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
final class LongestPalindromicSubstring {
|
final class LongestPalindromicSubstring {
|
||||||
private LongestPalindromicSubstring() {
|
private LongestPalindromicSubstring() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
/**
|
||||||
Solution s = new Solution();
|
* Finds the longest palindromic substring in the given string.
|
||||||
String str = "";
|
*
|
||||||
Scanner sc = new Scanner(System.in);
|
* @param s the input string
|
||||||
System.out.print("Enter the string: ");
|
* @return the longest palindromic substring
|
||||||
str = sc.nextLine();
|
*/
|
||||||
System.out.println("Longest substring is : " + s.longestPalindrome(str));
|
public static String longestPalindrome(String s) {
|
||||||
sc.close();
|
if (s == null || s.isEmpty()) {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Solution {
|
|
||||||
|
|
||||||
public String longestPalindrome(String s) {
|
|
||||||
if (s == null || s.length() == 0) {
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
int n = s.length();
|
|
||||||
String maxStr = "";
|
String maxStr = "";
|
||||||
for (int i = 0; i < n; ++i) {
|
for (int i = 0; i < s.length(); ++i) {
|
||||||
for (int j = i; j < n; ++j) {
|
for (int j = i; j < s.length(); ++j) {
|
||||||
if (isValid(s, i, j)) {
|
if (isValid(s, i, j) && (j - i + 1 > maxStr.length())) {
|
||||||
if (j - i + 1 > maxStr.length()) { // update maxStr
|
maxStr = s.substring(i, j + 1);
|
||||||
maxStr = s.substring(i, j + 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return maxStr;
|
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;
|
int n = hi - lo + 1;
|
||||||
for (int i = 0; i < n / 2; ++i) {
|
for (int i = 0; i < n / 2; ++i) {
|
||||||
if (s.charAt(lo + i) != s.charAt(hi - i)) {
|
if (s.charAt(lo + i) != s.charAt(hi - i)) {
|
||||||
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user