# Palindromic Substrings(dp)

Given a string, your task is to count how many palindromic substrings in this string.

The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.

**Example 1:**

```
Input:
 "abc"

Output:
 3

Explanation:
 Three palindromic strings: "a", "b", "c".
```

**Example 2:**

```
Input:
 "aaa"

Output:
 6

Explanation:
 Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".
```

**Note:**

1. The input string length won't exceed 1000.

分析

dp， 注意count 用法 dp.count(1)不行 count只能用在一元数组

```
class Solution:
    def countSubstrings(self, s):
        """
        :type s: str
        :rtype: int
        """
        if not s or len(s) == 0:
            return 0
        n = len(s)
        dp = [[0]*n for _ in range(n)]
        cnt = dp[0][0] = 1
        for i in range(1,n):
            dp[i][i] = 1
            cnt += 1
            for j in range(i):
                if s[i] == s[j] and (i == 1 + j or dp[j+1][i-1] == 1):
                    dp[j][i] = 1
                    cnt += 1
        return cnt
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://nataliekung.gitbook.io/ladder_code/facebook/palindromic-substringsdp.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
