Files
Java/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstring.java
Alex Klymenko c413f3c6b2 refactor: LongestPalindromicSubstring (#5420)
* refactor: LongestPalindromicSubstring

* checkstyle: fix formatting

---------

Co-authored-by: alxkm <alx@alx.com>
2024-08-28 18:35:21 +02:00

40 lines
1.3 KiB
Java

package com.thealgorithms.dynamicprogramming;
/**
* 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 String lps(String input) {
if (input == null || input.isEmpty()) {
return input;
}
boolean[][] arr = new boolean[input.length()][input.length()];
int start = 0;
int end = 0;
for (int g = 0; g < input.length(); g++) {
for (int i = 0, j = g; j < input.length(); i++, j++) {
if (g == 0) {
arr[i][j] = true;
} else if (g == 1) {
arr[i][j] = input.charAt(i) == input.charAt(j);
} else {
arr[i][j] = input.charAt(i) == input.charAt(j) && arr[i + 1][j - 1];
}
if (arr[i][j]) {
start = i;
end = j;
}
}
}
return input.substring(start, end + 1);
}
}