Palindromic Substrings

problem [https://leetcode.com/problems/palindromic-substrings/ 647. Palindromic Substrings].

Problem description
Given a string, 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
 * Input: s = "abc"
 * Output: 3
 * Explanation: Three palindromic strings: "a", "b", "c".


 * Example 2
 * Input: s = "aaa"
 * Output: 6
 * Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".


 * Constraints
 * consists of lowercase English letters.
 * consists of lowercase English letters.

Problem analysis
We can use Dynamic programming to solve the problem, by finding all the endpoints of the palindromic substrings.

We know that if a string is already a palindrome, then adding the same letter on both ends forms a longer palindrome.

Let  be the input string's length. We can create the  table with   rows and   columns, in which   will indicate whether   is a palindrome.

We can start from substrings with length 1, and increase the substring length stepwise until we cover the entire string.
 * Base cases:
 * If substring length is 1, then.
 * If substring length is 2, then  if the two characters are the same, or   otherwise.
 * Recursive case:
 * If substring length &ge; 3, then.

After computing all the cells in the table, the total number of  will be the result.

Complexity analysis
Let  be the input string's length.


 * Space complexity
 * We created the  table with   rows and   columns, so the space complexity is O( 2).


 * Time complexity
 * Since the amount of time to compute each cell and add a single value to the result is constant, the total time complexity is O( 2).