【Leetcode】647. Palindromic Substrings
Description
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:
- The input string length won't exceed 1000.
Discusss
回文字符串可以分为长度为1,2,3来分别讨论。dp[i][j]为回文字符串时,dp[i-1][j+1]是否为回文字符串只需判断i-1和j+1处的字符串是否相等。
所以可得状态方程dp[i][j] = dp[i + 1][j - 1] && c[i] == c[j] ? true : false;
Code
class Solution {
public int countSubstrings(String s) {
if (s == null || s.length() == 0) { return 0; }
int n = s.length();
boolean[][] f = new boolean[n][n];
f[0][0] = true;
char[] c = s.toCharArray();
int count = 1;
for (int i = 1; i < n; i++) {
f[i][i] = true;
count++;
if (c[i] == c[i-1]) {
f[i-1][i] = true;
count++;
}
}
for (int i = 2; i < n; i++) {
for (int j = 0; j < i - 1; j++) {
f[j][i] = f[j + 1][i - 1] && (c[j] == c[i] ? true : false);
if (f[j][i]) {
count++;
}
}
}
return count;
}
}
【Leetcode】647. Palindromic Substrings的更多相关文章
- 【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】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
- 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...
- 【Leetcode】Longest Palindromic Substring
问题:https://leetcode.com/problems/longest-palindromic-substring/ 给定一个字符串 S,求出 S 的最长回文子串 思路: 1. 回文:一个字 ...
- 【leetcode】Longest Palindromic Substring (middle) 经典
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【LeetCode】467. Unique Substrings in Wraparound String
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- 【leetcode】1190. Reverse Substrings Between Each Pair of Parentheses
题目如下: Given a string s that consists of lower case English letters and brackets. Reverse the strings ...
- 【LeetCode】1180. Count Substrings with Only One Distinct Letter 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 组合数 日期 题目地址:https://leetcod ...
随机推荐
- 笨办法学Python(四十一)
习题 41: 来自 Percal 25 号行星的哥顿人(Gothons) 你在上一节中发现 dict 的秘密功能了吗?你可以解释给自己吗?让我来给你解释一下,顺便和你自己的理解对比看有什么不同.这里是 ...
- Dll注入:修改PE文件 IAT注入
PE原理就不阐述了, 这个注入是PE感染的一种,通过添加一个新节注入,会改变PE文件的大小,将原有的导入表复制到新节中,并添加自己的导入表描述符,最后将数据目录项中指向的导入表的入口指向新节. 步骤: ...
- June 15th 2017 Week 24th Thursday
Whatever is worth doing is worth doing well. 任何值得做的,就把它做好. Whatever is worth doing is worth doing we ...
- javascript Object与Array用法
引用类型:引用类型是一种数据结构,用于将数据和功能组织在一起.引用类型的值是引用类型的一个实例. 一.Object ECMAScript中的对象其实就是一组数据和功能的结合. Object类型其实是所 ...
- POJ-3267 The Cow Lexicon---删除字符匹配单词
题目链接: https://cn.vjudge.net/problem/POJ-3267 题目大意: 题意就是给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列. PS: ...
- HashMap对HashCode碰撞的处理
先说Java之外的,什么是拉链法?怎么解决冲突的: 拉链法解决冲突的做法是:将所有关键字为同义词的结点链接在同一个单链表中. 若选定的散列表长度为m,则可将散列表定义为一个由m个头指针组成的指针数组t ...
- 【luogu P3901 数列找不同】 题解
对于区间查询的问题,提供一种思路: 莫队. 莫队是处理区间问题的乱搞神器,尤其是对于离线查询问题,当然也可以做在线查询,比如带修莫队. 对于有的题,莫队是乱搞骗分,而在某些地方,莫队是正解. 这道题来 ...
- 开始重学java【门头沟2017年12月6日】
现在从struts2开始重新学习java, 想找个人一起学习java/php都可以. 学习时间大概是两个月,就是年前这段时间. 下一个阶段就是做项目进行练习.(时间为:一个月时间) 不管是学习java ...
- Knowledge Point 20180305 数据在计算机中的表示
计算机发明的初衷就是用于帮助我们加工和处理数据,虽然时至今天计算机看起来无所不能,但它根本上还是在做数据的加工和处理,数据的机器层次表示将直接影响到计算机的结构和性能. 在计算机中,采用数字化方式来表 ...
- 模块socket使用
什么是socket:socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.我们无需再去深入理解tcp/udp协议,按照socket的规定去使用就行了. 首先一个c/s架构:分为两 ...