All problems

Problem 61

Count Palindromic Substrings

Medium · 20 pts · String, Dynamic Programming

Problem

You are given a string S. Count how many of its substrings are palindromes (read the same forwards and backwards). Substrings at different positions are counted separately, even if they are equal. Every single character is a palindrome by itself. For example, "aaa" has 6 palindromic substrings: three copies of "a", two of "aa" and one "aaa". Expanding around each possible centre (each character and each gap between characters) and counting every successful expansion gives an O(N²) solution.

Input

A single string S of lowercase English letters.

Output

Print the number of palindromic substrings.

Constraints

1 <= |S| <= 1000

Example input

abc

Example output

3

Explanation

In "abc", the single characters a, b and c are palindromes, but "ab", "bc" and "abc" are not, so there are 3 palindromic substrings.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Find All Anagrams in a String Next: Roman to Integer