# 647. Palindromic Substrings / Medium

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:

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:

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

# Solution 1: DP

## 思路

1. (s[i] == s[j]) && (j-i < 2)
• 可能是只有一個字元，如 a
• 兩個字元，如 aa
2. (s[i] == s[j]) && (dp[i+1][j-1] == true)
• 組成迴文的條件，最左和最右的字元相等，而且去掉最邊邊的兩個字元後，也是迴文。

## 效能

### Complexity

• Time Complexity: O(N^2)
• Space Complexity: O(N^2)

### LeetCode Result

• Runtime: 12 ms
• Memory Usage: 7.6 MB

# Solution 2: Brute Force

## 效能

### Complexity

• Time Complexity: O(N^2)
• Space Complexity: O(1)

### LeetCode Result

• Runtime: 0 ms
• Memory Usage: 13.3 MB

# Solution 3: Manacher Algorithm

## 效能

### Complexity

• Time Complexity: O(N)
• Space Complexity: O(N)

### LeetCode Result

• Runtime: 8 ms
• Memory Usage: 6.9 MB