Problem · String

Count Palindromic Substrings

Learn this problem
MediumThe Voleon Group logoThe Voleon GroupFULLTIMEPHONE SCREEN

Problem statement

Given a non-empty string s, return the number of its palindromic substrings.

A palindrome reads the same from left to right and from right to left. Count substrings by occurrence, so equal text at different positions counts separately. Every one-character substring is a palindrome.

Function

countPalindromicSubstrings(s: String) → int

Examples

Example 1

s = "abc"return = 3

The three one-character substrings are palindromes.

Example 2

s = "aaa"return = 6

The palindromic occurrences are three copies of a, two copies of aa, and one copy of aaa.

Example 3

s = "abba"return = 6

The four individual characters, bb, and abba are palindromes.

Constraints

  • 1 <= s.length <= 2000
  • s contains only lowercase English letters.
drafts saved locally
public int countPalindromicSubstrings(String s) {
    // Write your code here.
}
s"abc"
expected3
checking account