CJ

Explore

Tracks

My Courses

Edpresso

Refer a Friend

Create

Grokking Dynamic Programming Patterns for Coding Interviews

97% completed

Introduction

What is Dynamic Programming?

Pattern 1: 0/1 Knapsack

0/1 Knapsack

Equal Subset Sum Partition

Subset Sum

Minimum Subset Sum Difference

Count of Subset Sum

Target Sum

Pattern 2: Unbounded Knapsack

Unbounded Knapsack

Rod Cutting

Coin Change

Minimum Coin Change

Maximum Ribbon Cut

Pattern 3: Fibonacci Numbers

Fibonacci numbers

Staircase

Number factors

Minimum jumps to reach the end

Minimum jumps with fee

House thief

Pattern 4: Palindromic Subsequence

Longest Palindromic Subsequence

Longest Palindromic Substring

Count of Palindromic Substrings

Minimum Deletions in a String to make it a Palindrome

Palindromic Partitioning

Pattern 5: Longest Common Substring

Longest Common Substring

Longest Common Subsequence

Minimum Deletions & Insertions to Transform a String into another

Longest Increasing Subsequence

Maximum Sum Increasing Subsequence

Shortest Common Super-sequence

Minimum Deletions to Make a Sequence Sorted

Longest Repeating Subsequence

Subsequence Pattern Matching

Longest Bitonic Subsequence

Longest Alternating Subsequence

Edit Distance

Strings Interleaving

Appendix

Contact Us

Other courses

Mark Course as Completed

Count of Palindromic Substrings

We'll cover the following

Problem Statement

Solution

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".

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:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class CPS {

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) {
CPS cps = new CPS();
System.out.println(cps.findCPS("abdbca"));
System.out.println(cps.findCPS("cdpdd"));
System.out.println(cps.findCPS("pqr"));
}
}

RUN

SAVERESET

The time and space complexity of the above algorithm is O(n2)O(n^2)O(n​2​​), where ‘n’ is the length of the input string.

Completed

←    Back
Longest Palindromic Substring

Next    →
Minimum Deletions in a String to make it a Palindrome

Report an Issue

Ask a Question