When the length of s[i, j] is 1 or 2. For example, a or aa, it must be a palindrome.

When the length of s[i, j] is bigger than 2, it then depends on s[i+1, j-1], which is dp[i+1][j-1]. It also means that we need to iterate i from big to small.

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

A string is a palindrome when it reads the same backward as forward.

A substring is a contiguous sequence of characters within the string.

Example

1 2 3

Input: s = "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c".

/** * @param {string} s * @return {number} */ var countSubstrings = function(s) { if (s.length === 1) return1; let result = 1; for (let i = 1; i < s.length; i++) { for (let j = 0; j <= i; j++) { let str = s.slice(j, i + 1); if (isPalindrome(str)) { result++; } } } return result; };

functionisPalindrome(str) { let left = 0, right = str.length-1; while (left < right) { if (str[left++] !== str[right--]) { returnfalse; } } returntrue; }