class Solution:
def countSubstrings(self, s: str) -> int:
n = len(s)
dp = [[False] * n for _ in range(n)]
count = n
for i in range(n):
dp[i][i] = True
for j in range(1,n):
for i in range(j):
if j - i == 1 and s[i] == s[i+1]:
dp[i][i+1] = True
count += 1
if s[i] == s[j] and j - i > 1 and dp[i+1][j-1] == True:
dp[i][j] = True
count += 1
return count
刷题记录:647. 回文子串
最新推荐文章于 2025-03-05 21:35:01 发布