给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。
具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被计为是不同的子串。
示例 1:
输入: "abc" 输出: 3 解释: 三个回文子串: "a", "b", "c".
示例 2:
输入: "aaa" 输出: 6 说明: 6个回文子串: "a", "a", "a", "aa", "aa", "aaa".
注意:
输入的字符串长度不会超过1000。
C
int countSubstrings(char* s)
{
int n=strlen(s);
int count=0;
for(int i=0;i<n-1;i++)
{
int left=i-1;
int right=i+1;
while(left>=0 && right<n)
{
if(s[left]==s[right])
{
count++;
left--;
right++;
}
else
{
break;
}
}
left=i;
right=i+1;
while(left>=0 && right<n)
{
if(s[left]==s[right])
{
count++;
left--;
right++;
}
else
{
break;
}
}
}
return count+n;
}
python
class Solution:
def countSubstrings(self, s):
"""
:type s: str
:rtype: int
"""
n=len(s)
count=0
for i in range(0,n-1):
left=i-1
right=i+1
while left>=0 and right<n:
if s[left]==s[right]:
count+=1
left-=1
right+=1
else:
break
left=i
right=i+1
while left>=0 and right<n:
if s[left]==s[right]:
count+=1
left-=1
right+=1
else:
break
return count+n