【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 ...
随机推荐
- 【java开发系列】—— 深克隆和浅克隆
Java支持我们对一个对象进行克隆,通常用在装饰模式和原型模式中.那么什么是深克隆,什么是浅克隆呢. [浅克隆],通常只是对克隆的实例进行复制,但里面的其他子对象,都是共用的. [深克隆],克隆的时候 ...
- 基于ssh的多节点之间互信通信的实现
实现条件:node1:192.168.176.6 主机名称是node1.magedu.com: node2:192.168.176.6 主机名称是node1.magedu.com: 实现目的:在节点n ...
- Runloop理解
看了一堂公开课,自己小结一下: Runloop: 内部有三个东东:(Source, Timer, Observer) 作用/本质:1.死循环 (为app 保活): 2.监听处理事件 Timer 理解: ...
- HDU 2795 Billboard 【线段树维护区间最大值&&查询变形】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2795 Billboard Time Limit: 20000/8000 MS (Java/Others) ...
- PHP设计模式——装饰器模式
<?php /** * 装饰器模式 * 如果已有对象的部分内容或功能发生变化,但是不需要修改原始对象的结构,应使用装饰器模式 * * 为了在不修改对象结构的前提下对现有对象的内容或功能稍加修改, ...
- PHP设计模式——工厂模式
<?php /** * 工厂模式 * 提供获取某个对象的新实例的一个接口,同时使调用代码避免确定实际实例化基类的步骤. * * 工厂类用于创建不同类的实例,并将其返回. */ /** * 服务端 ...
- java 线程状态图
- File,FileInfo,Directory,DirectoryInfo
两者的共同点: 一:都用于典型的操作,如复制.移动.重命名.创建.打开.删除和追加到文件 二:默认情况下,将向所有用户授予对新文件的完全读/写访问权限. 两者的区别: File类是静态类, ...
- PAT (Basic Level) Practise (中文)-1040. 有几个PAT(25)
1040. 有几个PAT(25) http://www.patest.cn/contests/pat-b-practise/1040 字符串APPAPT中包含了两个单词“PAT”,其中第一个P ...
- Python—面向对象05 反射
反射,通过字符串映射到对象属性 class People: country='China' def __init__(self,name,age): self.name=name self.age ...