Solution Count of Palindromic Substrings
Problem Statement
Given a string, find the total number of palindromic substrings in it. Please note we need to find the total number of substrings and not subsequences.
Example 1:
Input: "abdbca"
Output: 7
Explanation: Here are the palindromic substrings, "a", "b", "d", "b", "c", "a", "bdb".
Example 2:
Input: = "cddpd"
Output: 7
Explanation: Here are the palindromic substrings, "c", "d", "d", "p", "d", "dd", "dpd".
Example 3:
Input: = "pqr"
Output: 3
Explanation: Here are the palindromic substrings,"p", "q", "r".
Constraints:
1 <= st.length <= 1000stconsists only of lowercase English letters.
Solution
This problem follows the "Longest Palindromic Subsequence" pattern, and can be easily converted to "Longest Palindromic Substring". The only difference is that instead of calculating the longest palindromic substring, we will instead count all the palindromic substrings.
Let's jump directly to the bottom-up dynamic programming solution:
class Solution {
public int findCPS(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()];
int count = 0;
// every string with one character is a palindrome
for (int i = 0; i < st.length(); i++) {
dp[i][i] = true;
count++;
}
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;
count++;
}
}
}
}
return count;
}
public static void main(String[] args) {
Solution cps = new Solution();
System.out.println(cps.findCPS("abdbca"));
System.out.println(cps.findCPS("cdpdd"));
System.out.println(cps.findCPS("pqr"));
}
}
The time and space complexity of the above algorithm is
🤖 Don't fully get this? Learn it with Claude
Stuck on Solution Count of Palindromic Substrings? 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 Count of Palindromic Substrings** (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 Count of Palindromic Substrings** 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 Count of Palindromic Substrings**. 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 Count of Palindromic Substrings**. 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.