题目描述
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 1:
1 | Input: "abc" |
Example 2:
1 | Input: "aaa" |
这题的第一反应就是用动态规划做。dp[i][j]表示从s[i]-s[j]的字符串是否为回文,如果为回文,则为1,反之0.
状态转移表达式为:
初始化为对任何i,$dp[i][i] =1$。最后,只需要计算出dp中有多少个1即可。
代码实现
1 | class Solution { |