问题描述(难度中等)
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" |
Note:
- The input string length won’t exceed 1000.
方法一:中心扩展法
选择中心进行扩展,中心可以分为两种情况,第一种是单个字符,第二种是两个字符。其他的情况都可以从这两种情况为中心的字符进行扩展。
1 | package P647; |
方法二:二维数组
二维数组保存结果。
1 | package P647; |
总结
中心扩展法思路比较简洁,可以作为算法扩展到其他的情形。