The Blind 75 Leetcode Series: Palindromic Substring
Given a string
s
, return the number of palindromic substrings in it.A string is a palindrome when it reads the same backward as forward.
A substring is a contiguous sequence of characters within the string.
Given a string, we need to find all substrings that are palindromic, meaning the word is spelled the same reading from beginning or the end.
Note that we don’t have to print out the list of words (though it’s a small change in the code should we do) but only count the number of such substrings.
Before starting, let’s make sure of a few things:
- For questions involving strings, always check what type of string this is. In this case, are we only expecting lowercase letters? If we do expect uppercase, do we count uppercase and lowercase of the same letter the same?
- How long can the string be?
- If we see the same substirng, do we count the repeats?
If the interviewer tells us that we can expect only lowercase letters, and the string is…