Facebook PixelPalindromic Substrings — Coding Practice
Palindromic SubstringsMedium

Palindromic Substrings

Medium 9.4k68% acceptance
Two PointersStringDynamic Programming

Given a string s, return the total number of palindromic substrings in it.

Every single character is a palindrome. Two substrings count separately if they start or end at different indices, even when they consist of the same letters.

Example 1
Input: s = "abc"
Output: 3
The palindromic substrings are "a", "b", and "c".
Example 2
Input: s = "aaa"
Output: 6
The palindromes are "a", "a", "a", "aa", "aa", and "aaa" — six in total, counting positions separately.
Constraints
  • 1 ≤ s.length ≤ 1000
  • `s` consists of lowercase English letters.
Asked atAmazonMetaGoogleMicrosoft
JavaScript
Loading editor…
Case 1
"abc"
expected: 3
Case 2
"aaa"
expected: 6
Case 3
"a"
expected: 1
Case 4
""
expected: 0
Case 5
"aaba"
expected: 6
Case 6
"abccba"
expected: 9
Case 7
"racecar"
expected: 10