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. jquery使用ajax报错[Uncaught SyntaxError: Unexpected token :]

    $.post('/ajax/validate.do',{"id": id},function(ret){ //ret }); 返回值明明是json,格式也是正确的,却解析不成功,在 ...

  2. Python学习-33.Python中glob模块的一些参数

    glob模块中有一个叫glob的方法可以获取某个目录下的文件. import glob temp=glob.glob("E:\\Temp\\*.txt") print(temp) ...

  3. python的数据存储

    Python存储数据 使用json.dump()和json.load() 不管专注的是什么,程序都把用户提供的信息存储在列表和字典等数据结构中.用户关闭程序时,你几乎总是要保存他们提供的信息:一种简单 ...

  4. TSQL--查找连续登陆用户

    --========================================== 需求:有一个用户登陆日志表,记录用户每次登陆时间,然后想查找用户按天连续登陆的情况,找出每次连续登陆的最早时间 ...

  5. 如何将Jenkins multiline string parameter的多行文本优雅的保存为文件

    [现象]: 使用multi-line string parameter获取的文本变量,在jenkins shell里面显示为单行文本(空格分割). [问题]:能否转换为多行文本,并存入文件. [解决方 ...

  6. C# 创建、部署和调用WebService简单示例

    webservice 可以用于分布式应用程序之间的交互,和不同程序之间的交互. 概念性的东西就不说太多,下面开始创建一个简单的webservice的例子.这里我用的是Visual Studio 201 ...

  7. Enum 绑定到 CheckBox

    第一种方法: 后台: internal static class EnumCache<T> where T : struct, IConvertible { private static ...

  8. SharePoint列表数据清除

    --获取站点对象 $spWeb =get-spweb http://123.sinochem.com --获取具体列表对象 $spList =$spWeb.GetListFromUrl("h ...

  9. windows下redis安装及应用

    一.下载安装Redis(windows版本) 1.下载地址:https://github.com/MicrosoftArchive/redis/releases 2.安装: 1)打开运行窗口,输出cm ...

  10. Java的入门知识和环境配置

    JVM(Java Virtual Machine)Java虚拟机 JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的. JAVA语言非常重要 ...