How I Dropped 50 lbs of Ugly Fat Without Cutting Off My Head

You know what the worst vice in the world is? AD-vice. So I’m not giving you any. I’m just going to tell you a story. Prologue: Once upon a time I trained with a woman named Mary, who had been a…


独家优惠奖金 100% 高达 1 BTC + 180 免费旋转

Palindromic Substrings

Palindrome Substring is a problem where you have to find the number of continuous substrings that should also be a palindrome. An example for that “aabcaa” the answer will be “8” because we can find 8 possible substrings that are considered palindromes

Throughout my learning journey, I used to use one definition to explain the palindrome:

But after reading for a while and solving many questions I came to a new clarification actually it’s not new but back then it was. The new definition says


There is a catch to this definition. If you are dealing with a string with an odd length because in this case, you can apply it with no problems. On the other hand, if your string has an even length then you can’t get the middle point for the cut, so you should pick two characters as middle points instead of one.

For Example :

1- “aba” → middle point will be index [1] and the cut will generate two substrings “a” and “a”

2- “abba” → if we pick only one middle point this will result in a wrong answer as the generated substrings will be not identical, although, the string is a palindrome!. So we will pick two middle points which are index 1,2 and will them against each other and keep going to generate the substrings “a” and “a”

Okay, I think for now we have a kind of base understanding of what is actually the palindrome. Let’s start to see this in the code

Example 1:

Example 2:

The first approach that came to my mind was the brute-force solution. It was at the top of my head when I read this question for the first time, the steps for this solution include generating all the substrings and checking each one of them if it’s a palindrome.

Need to do some improvements and this will take us to the second approach

To explain this part we need to take a step backward and check the second definition that I mentioned at the beginning of the article. Every palindrome is basically a mirror image around a central character. So we can say instead of generating all possible substrings and checking if any of them is a palindrome. We can do this in one step by defining two pointers to expand the search from the left and the right and check if they are identical or not and if not then do the same operation on the next character till we finish our string. The Steps for this approach will be

But this is not enough to get the correct answer because this actually just checking the odd palindrome length don't believe me :) let’s see

For Example:

As we can see we didn’t consider any even palindrome length the expansion follow this sequence 1,3,5,7,…. So, we need to consider the even length as well and this is easy we will just do the same work but with an extra one step

This solution works perfectly for me and btw this considers the best solution you can get in 45 minutes, especially since it has an O(N²) run time & O(1) space

If you like the content don’t forget to share it within your network ;)

Happy Problem-Solving folks, and stay around!

Add a comment

Related posts:

Buy Verified PayPal Accounts

We always have accounts in countries around the world ,USA,UK,AUS,CA. PayPal is an online payment system for USA companies. Millions of buyers and sellers can easily make payment transactions here…

MLB Announces Google Cloud As Official Cloud Partner

Google Cloud is the Official Cloud and Cloud Data and Analytics Partner of Major League Baseball. The multi-year partnership was announced on March 4th and included the amalgamating of the MLB…

Marionette v4 beta

The new version is on it’s way here and is a simpler API with plenty of performance improvements. We’ll be hard at work testing it in the wild during the beta, but unless anything surprising comes…