Leetcode 647. Palindromic Substrings
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:
- 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的更多相关文章
- [LeetCode] 647. Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- LeetCode 647. Palindromic Substrings的三种解法
转载地址 https://www.cnblogs.com/AlvinZH/p/8527668.html#_label5 题目详情 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同 ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python)
[LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...
- 【Leetcode】647. Palindromic Substrings
Description Given a string, your task is to count how many palindromic substrings in this string. Th ...
- 647. Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- 647. Palindromic Substrings 互文的子字符串
[抄题]: Given a string, your task is to count how many palindromic substrings in this string. The subs ...
- Manacher's Algorithm && 647. Palindromic Substrings 计算回文子串的算法
注:转载自:https://www.cnblogs.com/love-yh/p/7072161.html
- 647. Palindromic Substrings(马拉车算法)
问题 求一个字符串有多少个回文子串 Input: "abc" Output: 3 Input: "aaa" Output: 6 思路和代码(1)--朴素做法 用 ...
随机推荐
- (原创)hibernate 一对多建表实例详解 附上各个注释的含义
这个是hibernate的一对多建表实例:一的一端是部门(Department),对的一端是员工(Employee),下面贴上成员源代码:其中@mappedBy是加在@OneToMany一端,并且它的 ...
- alpha七天冲刺计划(更新ing)
alpha七天冲刺计划 第一天: http://note.youdao.com/noteshare?id=ff0c24feec21b1d74a176a0d88815933 第二天: http://no ...
- EBS FORM 编译
http://www.cnblogs.com/quanweiru/archive/2013/01/01/2841574.html EBS R11============================ ...
- git archive命令详解
git archive可以将加了tag的某个版本打包提取出来,例如: git archive -v --format= > v0..zip --format表示打包的格式,如zip,-v表示对应 ...
- Jersey构建restful风格的WebSerivices(二)
一. 总体说明 XML和JSON 是最为常用的数据交换格式.本例子演示如何将java对象,转成XML输出. 二.流程 1.在上文的例子中,创建一个包“com.waylau.rest.bean” 2.在 ...
- 蚂蚁男孩.队列组件(Framework.Mayiboy.Queue)
它能做什么 主要是用来方便使用队列而诞生,该组件封装了Queue和Redis中的队列,能够通过简单配置就可以高效快速使用起来. 使用说明 一. 下载源码,自己手动编译,手动引用必要的程序集.(需 ...
- .Net core 应用程序发布Web时,有些文件夹没有发布成功解决办法
如果文件是你在项目中手动添加的, 那么在解决方案中右击文件,然后点击属性,文件属性----高级----复制到输出目录----改为始终复制/如果较新则复制 即可.
- Prism 的 TabControl 导航
基于Prism 7.1 最近工作中可能会用到TabControl所以作为小菜的我提前预习了一下,结果并没有我想的那么简单,于是乎 各种网上查,本来用wpf的人就不多 prism 的可查的资料就更少的可 ...
- [UWP]爱恋动漫BT开发小记
在七月和某个人相识,在七月又和这个人重回陌路,在这个伤感的七月,让我来水一篇博客. 已经很久没有写博客了,最近现在来写一篇,总结一下这个七月. 今年的暑假特别的长,大概六月中旬就考完试了,而开学一直要 ...
- 对于equals和==的理解
很多时候equals和==大家都分不太清楚怎么样来使用,今天小编就来教大家怎么使用 equals比较的是两个变量的值是否相等 而==则比较的是这个变量的内存地址是否相同 打个比方来说 String a ...