Palindromic Substrings

You have been given a string STR. Your task is to find the total number of palindromic substrings of STR.

Example :
If the input string is "abbc", then all the possible palindromic substrings would be: ["a", "b", "b", c", "bb"] and hence, the output will be 5 since we have 5 substrings in total which form a palindrome.
Note :
A string is said to be a 'Palindrome' if it is read the same forwards and backwards. 
For example, “abba” is a palindrome, but “abbc” is not.

A 'Substring' is a contiguous sequence of characters within a string. 
For example, "a", "b", "c", "ab", "bc", "abc" are substrings of "abc".
Input format :
The first line contains an integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first and only line of each test case contains the string STR for which you have to count the number of palindromic substrings.
Output Format :
For each test case, return the total number of palindromic substrings possible.

Output for each test case will be printed in a separate line.
Note
You are not required to print anything, it has already been taken care of. Just implement the function.
Constraints :
1 <= t <= 100
0 <= N <= 1000

Where 't' is the number of test cases, 'N' is the length of the given string.
Time Limit: 1 sec.
CodingNinjas
author
2y

1. Brute Force
2. Optimised it using DP
3. Edge cases

CodingNinjas
author
2y
Brute Force Approach
  1. Initialize a count variable with 0 to keep track of the number of palindromic substrings.
  2. Let n be the length of the input string. Run a double nested loop. The outer loop will go f...read more
CodingNinjas
author
2y
Recursive Approach

The idea is to solve recursively or to put in other words, we are going to break the problem into smaller subproblems and take the decision on the basis of the solutions of these sma...read more

CodingNinjas
author
2y
Memoization

When we analyse the recursion tree for the previous solution, we find a lot of repetitive sub problems as the length of the string grows. Hence we can think of introducing the DP approach.

W...read more

CodingNinjas
author
2y
Iterative DP

Let's take a look at the recurrence relation for this problem:

First, let our string, str, be defined as

str = 

where str[i] is the i'th character of the st...read more

CodingNinjas
author
2y
Center Each Character

First, we consider the case of odd length substrings and check for palindromes.

We keep a global count initialized with 0.

1. Iterate over each character of the string

2. Consider ...read more

Add answer anonymously...
BNY Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter