Solution Longest Palindromic Substring
Problem Statement
Given a string, find the length of its Longest Palindromic Substring (LPS). In a palindromic string, elements read the same backward and forward.
Example 1:
Input: "abdbca"
Output: 3
Explanation: LPS is "bdb".
Example 2:
Input: = "cddpd"
Output: 3
Explanation: LPS is "dpd".
Example 3:
Input: = "pqr"
Output: 1
Explanation: LPS could be "p", "q" or "r".
Constraints:
1 <= st.length <= 1000stconsists only of lowercase English letters.
Basic Solution
This problem follows the "Longest Palindromic Subsequence" pattern. The only difference is that in a palindromic subsequence characters can be non-adjacent, whereas in a substring all characters should form a palindrome. We will follow a similar approach though.
The brute-force solution will be to try all the substrings of the given string. We can start processing from the beginning and the end of the string. So at any step, we will have two options:
- If the element at the beginning and the end are the same, we make a recursive call to check if the remaining substring is also a palindrome. If so, the substring is a palindrome from beginning to end.
- We will skip either the element from the beginning or the end to make two recursive calls for the remaining substring. The length of LPS would be the maximum of these two recursive calls.
Here is the code:
class Solution {
public int findLPSLength(String st) {
return findLPSLengthRecursive(st, 0, st.length() - 1);
}
private int findLPSLengthRecursive(String st, int startIndex, int endIndex) {
if (startIndex > endIndex) return 0;
// every string with one character is a palindrome
if (startIndex == endIndex) return 1;
// case 1: elements at the beginning and the end are the same
if (st.charAt(startIndex) == st.charAt(endIndex)) {
int remainingLength = endIndex - startIndex - 1;
// check if the remaining string is also a palindrome
if (
remainingLength ==
findLPSLengthRecursive(st, startIndex + 1, endIndex - 1)
) return remainingLength + 2;
}
// case 2: skip one character either from the beginning or the end
int c1 = findLPSLengthRecursive(st, startIndex + 1, endIndex);
int c2 = findLPSLengthRecursive(st, startIndex, endIndex - 1);
return Math.max(c1, c2);
}
public static void main(String[] args) {
Solution lps = new Solution();
System.out.println(lps.findLPSLength("abdbca"));
System.out.println(lps.findLPSLength("cddpd"));
System.out.println(lps.findLPSLength("pqr"));
}
}
Due to the three recursive calls, the time complexity of the above algorithm is exponential
Top-down Dynamic Programming with Memoization
We can use an array to store the already solved subproblems.
The two changing values to our recursive function are the two indexes, startIndex and endIndex. Therefore, we can store the results of all the subproblems in a two-dimensional array. (Another alternative could be to use a hash-table whose key would be a string (startIndex + "|" + endIndex))
Here is the code for this:
class Solution {
public int findLPSLength(String st) {
Integer[][] dp = new Integer[st.length()][st.length()];
return findLPSLengthRecursive(dp, st, 0, st.length() - 1);
}
private int findLPSLengthRecursive(
Integer[][] dp,
String st,
int startIndex,
int endIndex
) {
if (startIndex > endIndex) return 0;
// every string with one character is a palindrome
if (startIndex == endIndex) return 1;
if (dp[startIndex][endIndex] == null) {
// case 1: elements at the beginning and the end are the same
if (st.charAt(startIndex) == st.charAt(endIndex)) {
int remainingLength = endIndex - startIndex - 1;
// check if the remaining string is also a palindrome
if (
remainingLength ==
findLPSLengthRecursive(dp, st, startIndex + 1, endIndex - 1)
) {
dp[startIndex][endIndex] = remainingLength + 2;
return dp[startIndex][endIndex];
}
}
// case 2: skip one character either from the beginning or the end
int c1 = findLPSLengthRecursive(dp, st, startIndex + 1, endIndex);
int c2 = findLPSLengthRecursive(dp, st, startIndex, endIndex - 1);
dp[startIndex][endIndex] = Math.max(c1, c2);
}
return dp[startIndex][endIndex];
}
public static void main(String[] args) {
Solution lps = new Solution();
System.out.println(lps.findLPSLength("abdbca"));
System.out.println(lps.findLPSLength("cddpd"));
System.out.println(lps.findLPSLength("pqr"));
}
}
The above algorithm has a time and space complexity of
Bottom-up Dynamic Programming
Since we want to try all the substrings of the given string, we can use a two-dimensional array to store the subproblems' results. So dp[i][j] will be 'true' if the substring from index 'i' to index 'j' is a palindrome.
We can start from the beginning of the string and keep adding one element at a time. At every step, we will try all of its substrings. So for every endIndex and startIndex in the given string, we need to check the following thing:
If the element at the startIndex matches the element at the endIndex, we will further check if the remaining substring (from startIndex+1 to endIndex-1) is a substring too.
So our recursive formula will look like:
if st[startIndex] == st[endIndex], and if the remaing string is of zero length or dp[startIndex+1][endIndex-1] is a palindrome then dp[startIndex][endIndex] = true
Let's draw this visually for "cddpd", starting with a substring of length '1'. As we know, every substring with one element is a palindrome:

![startIndex:3, endIndex:4 => since st[startIndex] != st[endIndex] => false](/assets/5ee2fc0f008d9ff1.webp)
![startIndex:2, endIndex:3 => since st[startIndex] != st[endIndex] => false](/assets/39271bd9dfa5856a.webp)
![startIndex:2, endIndex:4 => since st[startIndex] == st[endIndex] and dp[startIndex+1][endIndex-1] is true => true](/assets/93776b4beb95c4f4.webp)
![startIndex:1, endIndex:2 => since st[startIndex] == st[endIndex] and it is a two character string => true](/assets/fa5e319bb3b7cce3.webp)
![startIndex:1, endIndex:3 => since st[startIndex] != st[endIndex] => false](/assets/7068668e667ddfa5.webp)
![startIndex:1, endIndex:4 => since st[startIndex] == st[endIndex] but dp[startIndex+1][endIndex-1] is false => false](/assets/86811e5750928b1e.webp)
![startIndex:0, endIndex:1-4 => since st[startIndex] != st[endIndex] => false](/assets/d9b7c4cfbf8b8399.webp)
Here is the code for our bottom-up dynamic programming approach:
class Solution {
public int findLPSLength(String st) {
// dp[i][j] will be 'true' if the string from index 'i' to index 'j' is a
// palindrome
boolean[][] dp = new boolean[st.length()][st.length()];
// every string with one character is a palindrome
for (int i = 0; i < st.length(); i++) dp[i][i] = true;
int maxLength = 1;
for (int startIndex = st.length() - 1; startIndex >= 0; startIndex--) {
for (int endIndex = startIndex + 1; endIndex < st.length(); endIndex++) {
if (st.charAt(startIndex) == st.charAt(endIndex)) {
// if it's a two character string or if the remaining string is a palindrome too
if (endIndex - startIndex == 1 || dp[startIndex + 1][endIndex - 1]) {
dp[startIndex][endIndex] = true;
maxLength = Math.max(maxLength, endIndex - startIndex + 1);
}
}
}
}
return maxLength;
}
public static void main(String[] args) {
Solution lps = new Solution();
System.out.println(lps.findLPSLength("abdbca"));
System.out.println(lps.findLPSLength("cdpdd"));
System.out.println(lps.findLPSLength("pqr"));
}
}
The time and space complexity of the above algorithm is
Manacher's Algorithm
The best-known algorithm to find the longest palindromic substring which runs in linear time
🤖 Don't fully get this? Learn it with Claude
Stuck on Solution Longest Palindromic Substring? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Solution Longest Palindromic Substring** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Solution Longest Palindromic Substring** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Solution Longest Palindromic Substring**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Solution Longest Palindromic Substring**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.