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.
https://leetcode.com/problems/palindromic-substrings/
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.
Test Cases
1 | class Solution: |
1 | import pytest |
Thoughts
这个跟 5. Longest Palindromic Substring 的处理逻辑几乎一致。
区别就是那个问题只记录最长的回文长度,而这个问题需要记录见到的所有回文的数量。
同样是从左到右遍历每个字符,对于当前字符,检查以其为中心能够构成的 palindromic 数量。
计数的时候注意,一个回文去掉首尾字符后的子回文也算一个。
Code
1 | class Solution: |