题目:
给定一个字符串,计算该字符串中有多少回文子字符串。
解题思路:
动态规划,类似之前的一个题目,直接给链接和代码
http://blog.csdn.net/xiaoxiaoley/article/details/77407263
代码(Python):
class Solution(object):
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
dist = [[0]*len(s) for i in range(len(s))]
def f(i,j):
if(j-i==0):
dist[i][j] = 1
return 1
if(((j-i)==1) & (s[i]==s[j])):
dist[i][j] = 1
return 1
if(((j-i)==1) & (s[i]!=s[j])):
dist[i][j] = 0
return 0
if((s[i]==s[j])&(dist[i+1][j-1]==1)):
dist[i][j] = 1
return 1
else:
dist[i][j] = 0
return 0
i = len(s)-1
j = 0;
while(j<=len(s)-1):
for k in range(len(s)-j):
f(k,k+j)
j=j+1
count = 0
for i in range(len(dist)):
for j in range(len(dist)):
if dist[i][j]:
count = count+1
return count