Proficient Level

Implement the method countPalindromicSubstrings that counts all palindromic substrings.

Count every contiguous substring that reads the same forward and backward.

The task is designed to test careful handling of edge cases, not only the most common input.

  • Single characters count as palindromic substrings.
  • Substrings with the same text but different positions are counted separately.
  • Comparison is case-sensitive.
Example 1
Input:
s (string) = abc
Return:
(int) 3
Example 2
Input:
s (string) = aaa
Return:
(int) 6
Example 3
Input:
s (string) = abccba
Return:
(int) 9

Expand around every possible center, including centers between two characters, and count matching expansions.

Run your code to see the result.