Palindromic Substrings

MediumDynamic Programming I: 1D

Given a string s, return how many of its substrings are palindromes. A substring is a contiguous run of characters, and substrings at different positions are counted separately even when their text is the same.

For example "aaa" has six palindromic substrings: three of length 1, two of length 2 and one of length 3.

Constraints

  • 1 <= s.length <= 1000
  • s consists of lowercase English letters.

Examples

Example 1

Input: s = "abc"
Output: 3

Only the three single letters are palindromes.

Example 2

Input: s = "aaa"
Output: 6

Six substrings: a (three times), aa (twice) and aaa.

Example 3

Input: s = "abba"
Output: 6

a, b, b, a, then "bb" and "abba": 6.

JavaScript you'll use: Strings and characters

/**
 * @param {string} s
 * @return {number}
 */
function countSubstrings(s) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.