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:

Input: "abc"
Output: 3
Explanation: Three palindromic strings: "a", "b", "c".

Example 2:

Input: "aaa"
Output: 6
Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".

Note:

  1. The input string length won't exceed 1000.

这道题给了一个字符串,让我们计算有多少个回文子字符串。博主看到这个题,下意识的想着应该是用 DP 来做,哼哼哧哧写了半天,修修补补,终于通过了,但是博主写的 DP 不是最简便的方法,略显复杂,这里就不贴了。还是直接讲解大神们的解法好了。其实这道题也可以用递归来做,而且思路非常的简单粗暴。就是以字符串中的每一个字符都当作回文串中间的位置,然后向两边扩散,每当成功匹配两个左右两个字符,结果 res 自增1,然后再比较下一对。注意回文字符串有奇数和偶数两种形式,如果是奇数长度,那么i位置就是中间那个字符的位置,所以左右两遍都从i开始遍历;如果是偶数长度的,那么i是最中间两个字符的左边那个,右边那个就是 i+1,这样就能 cover 所有的情况啦,而且都是不同的回文子字符串,参见代码如下:

解法一:

class Solution {
public:
int countSubstrings(string s) {
if (s.empty()) return ;
int n = s.size(), res = ;
for (int i = ; i < n; ++i) {
helper(s, i, i, res);
helper(s, i, i + , res);
}
return res;
}
void helper(string s, int i, int j, int& res) {
while (i >= && j < s.size() && s[i] == s[j]) {
--i; ++j; ++res;
}
}
};

在刚开始的时候博主提到了自己写的 DP 的方法比较复杂,为什么呢,因为博主的 dp[i][j] 定义的是范围 [i, j] 之间的子字符串的个数,这样其实还需要一个二维数组来记录子字符串 [i, j] 是否是回文串,那还不如直接就将 dp[i][j] 定义成子字符串 [i, j] 是否是回文串就行了,然后i从 n-1 往0遍历,j从i往 n-1 遍历,然后看 s[i] 和 s[j] 是否相等,这时候需要留意一下,有了 s[i] 和 s[j] 相等这个条件后,i和j的位置关系很重要,如果i和j相等了,则 dp[i][j] 肯定是 true;如果i和j是相邻的,那么 dp[i][j] 也是 true;如果i和j中间只有一个字符,那么 dp[i][j] 还是 true;如果中间有多余一个字符存在,则需要看 dp[i+1][j-1] 是否为 true,若为 true,那么 dp[i][j] 就是 true。赋值 dp[i][j] 后,如果其为 true,结果 res 自增1,参见代码如下:

解法二:

class Solution {
public:
int countSubstrings(string s) {
int n = s.size(), res = ;
vector<vector<bool>> dp(n, vector<bool>(n));
for (int i = n - ; i >= ; --i) {
for (int j = i; j < n; ++j) {
dp[i][j] = (s[i] == s[j]) && (j - i <= || dp[i + ][j - ]);
if (dp[i][j]) ++res;
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/647

类似题目:

Longest Palindromic Subsequence

Longest Palindromic Substring

参考资料:

https://leetcode.com/problems/palindromic-substrings/

https://leetcode.com/problems/palindromic-substrings/discuss/105689/Java-solution-8-lines-extendPalindrome

https://leetcode.com/problems/palindromic-substrings/discuss/105688/Very-Simple-Java-Solution-with-Detail-Explanation

https://leetcode.com/problems/palindromic-substrings/discuss/105707/Java-DP-solution-based-on-longest-palindromic-substring

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] 647. Palindromic Substrings 回文子字符串的更多相关文章

  1. [LeetCode] Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  2. Java实现 LeetCode 730 统计不同回文子字符串(动态规划)

    730. 统计不同回文子字符串 给定一个字符串 S,找出 S 中不同的非空回文子序列个数,并返回该数字与 10^9 + 7 的模. 通过从 S 中删除 0 个或多个字符来获得子字符序列. 如果一个字符 ...

  3. [Swift]LeetCode730. 统计不同回文子字符串 | Count Different Palindromic Subsequences

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  4. LeetCode Valid Palindrome 有效回文(字符串)

    class Solution { public: bool isPalindrome(string s) { if(s=="") return true; ) return tru ...

  5. 647. Palindromic Substrings 互文的子字符串

    [抄题]: Given a string, your task is to count how many palindromic substrings in this string. The subs ...

  6. LeetCode-5:Longest Palindromic Substring(最长回文子字符串)

    描述:给一个字符串s,查找它的最长的回文子串.s的长度不超过1000. Input: "babad" Output: "bab" Note: "aba ...

  7. Leetcode 647. Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  8. [LeetCode] Count Binary Substrings 统计二进制子字符串

    Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...

  9. LeetCode 647. Palindromic Substrings的三种解法

    转载地址 https://www.cnblogs.com/AlvinZH/p/8527668.html#_label5 题目详情 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同 ...

随机推荐

  1. 【Linux命令】at、crontab定时任务

    crontab定时任务(周期性) 一.简介 通过crontab命令可以在固定的间隔执行系统指令或shell脚本 二.crontab配置文件: Linux下的任务调度分为两类:系统任务调度和用户任务调度 ...

  2. CentOS 下安装 Cmake 步骤

    最近在虚拟机中的 CentOS 中安装 Cmake.把安装步骤记录在此. 什么是 Cmake CMake 是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程).他能够输出各 ...

  3. Vue.js 源码分析(二十四) 高级应用 自定义指令详解

    除了核心功能默认内置的指令 (v-model 和 v-show),Vue 也允许注册自定义指令. 官网介绍的比较抽象,显得很高大上,我个人对自定义指令的理解是:当自定义指令作用在一些DOM元素或组件上 ...

  4. RESTful及API设计(原)

    RESTful是一种架构风格,是由Fielding博士在自己的博士论文中提出并详细论述的. 它是用于指导web系统设计的,而指导API设计只是它的一小部分功能而已,如果只用它指导API设计就太大材小用 ...

  5. 【USTC】雨

    自九月初来到科大,到现在已经一个月.这几天是国庆假期(祖国七十华诞,祝福祖国),我没有回家,白天在实验室,晚上去找小许. 今天下雨了,不大,但是温度降了大约10度,上次下雨还是九月初开学那几日. 9月 ...

  6. 基于SqlClient开发SQLServer工具类 伸手党的福音

    注意:代码直接Copy调用即可,SQLServer工具类跟我上一个Oracle工具类有所不同,区别在于调用存储过程中时参数的使用,无需输入对应的存储游标名称 特点:根据用户传入的参数类型来识别存储中对 ...

  7. 打造游戏金融小程序行业测试标准腾讯WeTest携各专家共探品质未来

    在获客成本不断上升的时代里,产品品质愈发是互联网应用的决胜标准.随着用户需求更加多样,开发者不仅要深挖应用功能,更需要面向业务所在领域,建立全面.专业的测试架构,掌控开发进度.提高开发效率,才能在互联 ...

  8. 微信小程序 自定义头部导航栏和导航栏背景图片 navigationStyle

    ​ 这两天因为要做一个带背景的小程序头,哭了,小程序导航栏有背景也就算了,还得让导航栏上的背景顺下来,心态小崩.现在可以单独设置一个页面的小程序头了,但是前提是要微信7.0以上的版本,考虑到兼容性问题 ...

  9. AJAX小示例

    一. 基本内容 定义:AJAX(Asynchronous Javascript And XML)翻译成中文就是"异步的Javascript和XML",即使用Javascript语言 ...

  10. AutoresizingMask 的使用

    (1)先了解一下这几个枚举值的含义: (2)代码演说: 在viewcontroller 用代码创建一个红色的view,如下: UIView *redView = [[UIView alloc] ini ...