This problem is similar to 5. Longest Palindromic Substring
This is a counting problem because the problem asks us to calculate the number of palindromic substrings. We can achieve that with “expandFromCenter” technique.
Here, we iterate over all characters and try to expand from the center of each character and check if the substring is a palindrome. Unlike leetcode 5 where we need to find the longest substring, here we need to calculate how many of them exist: hence we use the counter and count both even and odd length palindromes.
Code:
Java
Big O Analysis
-
Runtime
The runtime complexity here is .
-
Memory
The memory usage is since we aren’t using any extra datastructure.
— A