Master the Longest Palindromic Substring Problem with DP in Java & C++
After a humorous anecdote about job background checks, this article dives into LeetCode's classic Longest Palindromic Substring problem, explains brute‑force, center‑expansion, and dynamic‑programming approaches, and provides complete Java and C++ implementations with detailed DP recurrence and traversal strategies.
Problem Description
Given a string s, find the longest palindromic substring. A palindrome reads the same forward and backward.
Examples
Input: s = "babad" Output: "bab" Explanation: "aba" is also a valid answer.
Input: s = "cbbd" Output: "bb"
1 ≤ s.length ≤ 1000
s consists only of digits and English letters.
Problem Analysis
The simplest solution enumerates all substrings and checks each for palindrome, which is O(n³). A better approach is center expansion, but still O(n²). The most efficient classic solution is the Manacher algorithm, but here we focus on a dynamic programming (DP) method.
Dynamic Programming Solution
Define dp[left][right] as true if the substring s[left…right] is a palindrome. The recurrence is:
dp[left][right] = (s[left] == s[right]) && (right - left <= 2 || dp[left+1][right-1])When computing dp[left][right], dp[left+1][right-1] must already be known, so we iterate the table from shorter substrings to longer ones (e.g., by increasing right and decreasing left, or by diagonal order).
Java Implementation
public String longestPalindrome(String s) {
int start = 0, maxLen = 1;
int length = s.length();
boolean[][] dp = new boolean[length][length];
for (int right = 1; right < length; right++) {
for (int left = 0; left < right; left++) {
if (s.charAt(left) != s.charAt(right)) continue;
if (right - left <= 2) {
dp[left][right] = true;
} else {
dp[left][right] = dp[left + 1][right - 1];
}
if (dp[left][right] && right - left + 1 > maxLen) {
maxLen = right - left + 1;
start = left;
}
}
}
return s.substring(start, start + maxLen);
}C++ Implementation
string longestPalindrome(string s) {
int start = 0, maxLen = 1;
int length = s.size();
vector<vector<int>> dp(length, vector<int>(length));
for (int right = 1; right < length; right++) {
for (int left = 0; left < right; left++) {
if (s[left] != s[right]) continue;
if (right - left <= 2) {
dp[left][right] = true;
} else {
dp[left][right] = dp[left + 1][right - 1];
}
if (dp[left][right] && right - left + 1 > maxLen) {
maxLen = right - left + 1;
start = left;
}
}
}
return s.substr(start, maxLen);
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
