> For the complete documentation index, see [llms.txt](https://nataliekung.gitbook.io/ladder_code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nataliekung.gitbook.io/ladder_code/twitter-oa/palindromic-substrings.md).

# Palindromic Substrings

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

**Example1**

```
Input: "abc"
Output: 3
Explanation:
3 palindromic strings: "a", "b", "c".
```

**Example2**

```
Input: "aba"
Output: 4
Explanation:
4 palindromic strings: "a", "b", "a", "aba".
```

#### Notice

The input string length won't exceed 1000

分析

以中间2位向两边扩展，注意判断left, right 是在范围内

```
class Solution:
    """
    @param str: s string
    @return: return an integer, denote the number of the palindromic substrings
    """
    def countPalindromicSubstrings(self, str):
        # write your code here
        n = len(str)
        def expand(i,j):
            cnt = 0
            while 0 <= i and j < n:
                if str[i] != str[j]:
                    break
                i -= 1 
                j += 1
                cnt += 1
            return cnt
            
        res = 0
        for i in range(n):
            res += expand(i,i)
            res += expand(i,i+1)
        return res

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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/twitter-oa/palindromic-substrings.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.
