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)--朴素做法 用 ...
随机推荐
- ksplatform学习笔记
1.viewResolver配置中的: <bean id="viewResolver" class="org.springframework.web.servlet ...
- Banana
Banana 除了有香蕉的意思外,还表示 喜剧演员
- 取Mac地址
uses Nb30; //一般用默认的 0 就可以了 ):string; var ncb : TNCB; {NetBios控制块} AdapterS : TAdapterStatus; {网卡状态结构 ...
- 1.WebApi介绍
1.WebApi是什么: WebAPI 是一种用来开发系统间接口.设备接口 API 的技术,基于 Http 协议,请求和返 回格式结果默认是 json 格式.比 WCF 更简单.更通用,比 WebSe ...
- sql 中如何查询某一列的数据在另一个表中有没有?
假设表table1,列a,表table2,列bselect a from table1where a not in(select b from table2)
- BitAdminCore框架更新日志20180523
20180523更新内容 本次更新两个内容,一是增加视频处理功能,二是增加定时服务功能. 视频处理 定时服务 BitAdminCore框架,用最少的代码,实现最多的功能 本次新暂未发布,后续有空发布 ...
- 【Selenium专题】元素定位之一简单定位
UI自动化工具千变万化.架构千变万化,但都逃离不开的关键一步就是元素定位.下面以Selenium为例介绍常见的几个元素定位方法 ID -元素id属性 WebElement El = driver.fi ...
- day 100天 VUE 父子传值,单页面.
一 .静态资源导入方法 启动服务 npm run serve <template> <div id ="app"> <h3>{{msg}}& ...
- *p++与(*p)++与*(p++)------自增运算符常见误区
自增运算符(++) 自增\自减运算符分为前缀形(++a)和后缀形(a++),这里重点分析自增 大部分人对前缀和后缀的理解一般是,前缀形式是先++再使用(先变后用),后缀形式是先使用再++(先用后变) ...
- ZZNU 1719(最长上升子序列+最长下降子序列)
先吐血一发,噗! 再吐血一次,啊啊啊啊! 好吧,做了那么多次最长上升子序列,看这题看了半天才发现还有最长下降子序列,呵呵哒! AC代码: #include<stdio.h>//老恶心#in ...