14.1 Longest Palindromic Substring
14.1.1 Problem Metadata
- Platform: LeetCode
- Problem ID: 5
- Difficulty: Medium
- URL: https://leetcode.com/problems/lc-longest-palindromic-substring/
- Tags: Grind 75, Blind 75, NeetCode 150
- Techniques: Dynamic Programming, Tabulation, String
14.1.5 Solution - DP via Reversed String
14.1.5.1 Walkthrough
Compute the longest common substring between s and its reverse. Matches correspond to palindromic substrings only when their indices line up (i.e., the substring reads the same forward and backward). Track the candidate with the greatest length that satisfies the index alignment rule.
14.1.5.3 Implementation Steps
- Let
rev = reverse(s); createdp[n][n]. - For each pair
(i, j)updatedp[i][j]similar to LCS-substring. - When
dp[i][j] > currentMax, ensure the substring indices align (n - 1 - j + dp[i][j] - 1 == i). - Update best boundaries accordingly.
14.1.5.4 Code - Java
public String longestPalindrome(String s) {
int n = s.length();
String rev = new StringBuilder(s).reverse().toString();
int[][] dp = new int[n][n];
int maxLen = 0;
int end = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s.charAt(i) == rev.charAt(j)) {
if (i == 0 || j == 0) {
dp[i][j] = 1;
} else {
dp[i][j] = dp[i - 1][j - 1] + 1;
}
int beforeRev = n - 1 - j;
if (dp[i][j] > maxLen && beforeRev + dp[i][j] - 1 == i) {
maxLen = dp[i][j];
end = i;
}
} else {
dp[i][j] = 0;
}
}
}
return s.substring(end - maxLen + 1, end + 1);
}