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:

  1. 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的更多相关文章

  1. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

  2. 【LeetCode】647. Palindromic Substrings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力循环 方法二:固定起点向后找 方法三:动 ...

  3. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  4. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

  5. 【Leetcode】Longest Palindromic Substring

    问题:https://leetcode.com/problems/longest-palindromic-substring/ 给定一个字符串 S,求出 S 的最长回文子串 思路: 1. 回文:一个字 ...

  6. 【leetcode】Longest Palindromic Substring (middle) 经典

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. 【LeetCode】467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  8. 【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 ...

  9. 【LeetCode】1180. Count Substrings with Only One Distinct Letter 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 组合数 日期 题目地址:https://leetcod ...

随机推荐

  1. 【Leetcode】【Medium】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

  2. Appium的DesiredCapabilities参数设置

    Appium的DesiredCapabilities参数设置 DesiredCapabilities 负责启动服务端时的参数设置.实际使用时根据自己的需要,可自行修改一些参数. 比如,应用程序在查找某 ...

  3. ABAP宏的调试

    我们都知道高级语言宏一般是无法调试的.但是ABAP的宏例外. 比如我写了下面一段宏,名为insert_table. 执行这段代码,调试器会在第23行停下来. ABAP调试器里有个工具可以用于宏的调试, ...

  4. 生理周期,POJ(1006)

    题目链接:http://poj.org/problem?id=1006 解题报告: 1.枚举天数的时候可以根据前面的结果直接跳过一些错误的答案. ///三个周期是23,28,33, #include ...

  5. 动态规划(DP),最大矩阵和

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=74 http://poj.org/problem?id=1050 解题 ...

  6. 【[COCI2011-2012#5] POPLOCAVANJE】

    据说这道题卡空间? 不存在的,拿\(AC\)自动机去存\(5000\times5000\)的串肯定是要M的 我们可以考虑对长度为\(n\)的串建一个\(SAM\),这样空间就只需要两倍的\(3e5\) ...

  7. 【[SCOI2012]喵星球上的点名】

    好题啊 \(SA+ST\text{表}+\text{莫队}\) 我们先强行把所有的串连起来,串与串之间插入特殊字符,姓和名之间也插入特殊字符 之后跑一遍\(SA\),求出\(sa\)和\(het\) ...

  8. 【JeeSite】角色和权限的修改

    @RequiresPermissions("sys:role:edit") @RequestMapping(value = "save") public Str ...

  9. Linux 安装ruby编译环境

    1.输入:yum install ruby 1.1如果安装文件出错Error Downloading Packages: 输入:yum clean all 输入:yum makecache,此时如果出 ...

  10. 解决div+img布局下img下端出现空白的bug

    1.将图片转换为块级对象 即设置img为“display:block;”.在本例中添加一组CSS代码:“#sub img {display:block;}”. 2.设置图片的垂直对齐方式 即设置图片的 ...