Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string.
The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters.
Example
Input: “abc”
Output: 3
Explanation: Three palindromic strings: “a”, “b”, “c”.
Solution
DP的思路是 对于dp[i][j] 检验dp[i-1][j+1]和s[i]==s[j]是否同时成立
class Solution:
def countSubstrings(self, s: str) -> int:
N = len(s)
ret = 0
for i in range(2*N-1):
l = i//2
r = (i+1)//2
while l>=0 and r<=N-1 and s[l]==s[r]:
l -= 1
r += 1
ret += 1
return ret