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:
Explanation: Three palindromic strings: "a", "b", "c".

Example 2:

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

Note:

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

Similar Questions: Longest Palindromic Substring Longest Palindromic Subsequence Palindromic Substrings

Next challenges: Longest Palindromic Subsequence

方法一:O(n^2)的时间复杂度。

思路:回文字符串有奇数个字符时,当前i位置字母作为回文字符串的中心;有偶数个字符的时候当前i和i+1位置字母作为回文字符串的中心。

代码:

 public class Solution {
public int countSubstrings(String s) {
if(s == null) return 0;
int count = 0;
for(int i = 0; i < s.length(); i++) {
count += countPalindromicSubstrings(s, i, i);
count += countPalindromicSubstrings(s, i, i + 1);
}
return count;
} public int countPalindromicSubstrings(String s, int begin, int end) {
int count = 0;
while(begin >= 0 && end < s.length() && s.charAt(begin) == s.charAt(end)) {
count++;
begin--;
end++;
}
return count;
}
}

方法二:O(n)的时间复杂度。

思路:先对字符串进行改造(例如原字符串是"bab",改造后是"#b#a#b#"),接着对改造后的字符串运行Manacher's Algorithm(“马拉车”算法),得到以s[i]为中心的回文串的半径RL[i](不包括中心。例如"a"的半径就是0;"bab"以"a"为中心,半径就是1),显然,以s[i]为中心,RL[i]为半径的回文串中含有的字回文串数目是(RL[i] + 1) / 2个。最后只要将每个(RL[i] + 1) / 2加和就是结果。

关于Manacher's Algorithm的学习资料:

https://segmentfault.com/a/1190000003914228

http://www.cnblogs.com/grandyang/p/4475985.html

代码:

 public class Solution {
public int countSubstrings(String s) {
String rs = "#";
//改造
for(int i = 0; i < s.length(); i++) rs = rs + s.charAt(i) + "#";
int[] RL = new int[rs.length()];//半径
int pos = 0, maxRight = 0, count = 0;
for(int i = 0; i < rs.length(); i++) {
if(i < maxRight) {
RL[i] = Math.min(maxRight - i, RL[2 * pos - i]);
}
while(i - RL[i] - 1 >= 0 && i + RL[i] + 1< rs.length() && rs.charAt(i - RL[i] - 1) == rs.charAt(i + RL[i] + 1)) {
RL[i]++;
}
if(i + RL[i] > maxRight) {
pos = i;
maxRight = i + RL[i];
}
count += (RL[i] + 1) / 2;
}
return count;
}
}

Leetcode 647. Palindromic Substrings的更多相关文章

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

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

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

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

  3. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  4. 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...

  5. 【Leetcode】647. Palindromic Substrings

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

  6. 647. Palindromic Substrings

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

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

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

  8. Manacher's Algorithm && 647. Palindromic Substrings 计算回文子串的算法

    注:转载自:https://www.cnblogs.com/love-yh/p/7072161.html

  9. 647. Palindromic Substrings(马拉车算法)

    问题 求一个字符串有多少个回文子串 Input: "abc" Output: 3 Input: "aaa" Output: 6 思路和代码(1)--朴素做法 用 ...

随机推荐

  1. node express session

    在express4.0版本以上,需要单独增加session模块:express-session:https://www.npmjs.com/package/express-session 具体做法是, ...

  2. RelativeLayout中最底的View一个View.layout_marginBottom无效

    处理一个Dialog,发现RelativeLayout布局下最后一个View的layout_marginBottom会失效. 效果图见: 解决方法为: 在最底或最右的组件后面再加个View吧... 这 ...

  3. Delphi for iOS开发指南(1):在Mac上配置你的开发环境

    http://cache.baiducontent.com/c?m=9d78d513d99516f11ab7cf690d678c3b584380122ba7a0020fd18438e4732b4050 ...

  4. WPF/Silverlight开发的15个最佳实践(转发)

    英文出处:http://www.kunal-chowdhury.com/2010/08/some-best-practices-for-silverlight.html 作者:Kunal Chowdh ...

  5. 哎呀,我艹,使用tfs时候,离职人员锁定了代码.

    哎呀,我艹,使用tfs时候,离职人员锁定了代码. 而且离职人员电脑已经回收,被格式化了,怎么破? 不管别人是有意,还是无意,总之就是需要搞定了. 1.第一步 首先,找到被锁住的工作区一般在报错信息中可 ...

  6. golang 编译为dll 的方法

    之前一直再找如何将geojson 转为 svg 格式的数据,看到github上大多都是js来转的,只有一篇是golang来做的,想来把它封成dll 给c#.c++ 调用,网上查了很多方法,并没有写的很 ...

  7. BootStrap 4正式版发布(原文翻译)

    原文链接:http://blog.getbootstrap.com/2018/01/18/bootstrap-4/ 关于Bootstrap 什么是Bootstrap Bootstrap,来自 Twit ...

  8. 六,mysql优化——小知识点

    1,选择适当的字段类型,特别是主键 选择字段的一般原则是保小不保大,能占用字节小的字段就不用大字段.比如主键,建议使用自增类型,这样节省空间,空间就是效率!按4个字节和按32个字节定位一条记录,谁快谁 ...

  9. Java并发工具类之CountDownLatch

    CountDownLatch允许一个或则多个线程等待其他线程完成操作. 假如我们有这样的需求:我们需要解析一个excel文件中的多个sheet,我们可以考虑使用多线程,每一个线程解析excel中的一个 ...

  10. Storm集群参数调整

    Supervisor 参数调整 修改${STORM_HOME}conf/storm.yaml文件内容 supervisor变更参数 slots 配置: 若storm host仅仅执行superviso ...