> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Palindromic Substrings Python Solution

> Tested Python solution for LeetCode 647 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 647, Medium. Topics: Two Pointers, String, Dynamic Programming. [View on LeetCode](https://leetcode.com/problems/palindromic-substrings/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 647   # by problem number
lcpy gen -s palindromic_substrings   # by problem name
```

## Problem

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.

### Examples

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

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

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/palindromic_substrings/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/palindromic_substrings/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2) - expand around centers approach
    # Space: O(1) - no extra space used
    def count_substrings(self, s: str) -> int:
        """
        Count palindromic substrings using expand around centers approach.
        For each possible center (single char or between two chars), expand outward
        and count palindromes.
        """
        if not s:
            return 0

        count = 0
        n = len(s)

        for i in range(n):
            # Odd length palindromes (center at i)
            count += self._expand_around_center(s, i, i)

            # Even length palindromes (center between i and i+1)
            count += self._expand_around_center(s, i, i + 1)

        return count

    def _expand_around_center(self, s: str, left: int, right: int) -> int:
        """Expand around center and count palindromes."""
        count = 0
        while left >= 0 and right < len(s) and s[left] == s[right]:
            count += 1
            left -= 1
            right += 1
        return count
```

## Complexity

| Time                                    | Space                      |
| --------------------------------------- | -------------------------- |
| O(n^2) - expand around centers approach | O(1) - no extra space used |

## Tags

[Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
