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.
Example 1:
Input: s = "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c".
Example 2:
Input: s = "aaa" Output: 6 Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
Constraints:
1 <= s.length <= 1000
s
consists of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding palindromic substrings is all about checking everything. We look at every possible piece of the given text, and for each piece, we check if it's a palindrome (reads the same forwards and backward). Then we count how many of these palindromic pieces we've found.
Here's how the algorithm would work step-by-step:
def count_palindromic_substrings_brute_force(input_string):
number_of_palindromic_substrings = 0
string_length = len(input_string)
# Iterate over all possible start indices
for start_index in range(string_length):
# Iterate over all possible end indices for each start index
for end_index in range(start_index, string_length):
substring = input_string[start_index:end_index + 1]
# Check if the substring is a palindrome
if substring == substring[::-1]:
#Increment if palindrome condition is met
number_of_palindromic_substrings += 1
return number_of_palindromic_substrings
Instead of checking every possible substring, we can find all palindromic substrings efficiently by expanding around each character and pair of adjacent characters. This method smartly reuses previously discovered palindromes to avoid redundant checks. This 'expand from the middle' approach drastically reduces the work needed.
Here's how the algorithm would work step-by-step:
def count_palindromic_substrings(input_string):
string_length = len(input_string)
palindrome_count = 0
for i in range(string_length):
# Check for odd length palindromes
left_index = i
right_index = i
# Expand around the center to find palindromes.
while left_index >= 0 and right_index < string_length and input_string[left_index] == input_string[right_index]:
palindrome_count += 1
left_index -= 1
right_index += 1
# Check for even length palindromes
left_index = i
right_index = i + 1
# Expand around the center to find palindromes.
while left_index >= 0 and right_index < string_length and input_string[left_index] == input_string[right_index]:
# Found another palindrome.
palindrome_count += 1
left_index -= 1
right_index += 1
return palindrome_count
Case | How to Handle |
---|---|
Null or Empty String Input | Return 0 if the input string is null or empty as there are no substrings. |
Single Character String | A single character string is always a palindrome, so return 1. |
String with all identical characters (e.g., 'aaaa') | The number of palindromic substrings should be n*(n+1)/2, handled correctly by expanding around each character. |
String with alternating characters (e.g., 'abab') | The algorithm should identify palindromes like 'a', 'b', 'aba', 'bab', and 'aba'. |
Very Long Input String | Ensure the solution's time complexity is optimized (e.g., O(n^2) using dynamic programming or expanding around centers) to avoid Time Limit Exceeded errors. |
String contains special characters or spaces | The solution should correctly identify palindromes even with special characters or spaces. |
String with only two different characters repeated multiple times (e.g., 'aabbbaa') | The algorithm needs to identify both single-character and longer palindromes formed by these repeating characters. |
String with maximum length constraint | Verify there is no integer overflow when calculating string lengths or substring boundaries. |