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".
 class Solution {
public int countSubstrings(String s) {
int n = s.length();
int res = ;
boolean[][] dp = new boolean[n][n];
for (int i = n - ; i >= ; i--) {
for (int j = i; j < n; j++) {
dp[i][j] = s.charAt(i) == s.charAt(j) && (j - i < || dp[i + ][j - ]);
if(dp[i][j]) ++res;
}
}
return res;
}
} public class Solution {
int count = ; public int countSubstrings(String s) {
if (s == null || s.length() == ) return ; for (int i = ; i < s.length(); i++) { // i is the mid point
extendPalindrome(s, i, i); // odd length;
extendPalindrome(s, i, i + ); // even length
}
return count;
} private void extendPalindrome(String s, int left, int right) {
while (left >= && right < s.length() && s.charAt(left) == s.charAt(right)) {
count++;
left--;
right++;
}
}
}

Palindromic Substrings的更多相关文章

  1. [LeetCode] Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  2. [Swift]LeetCode647. 回文子串 | Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  3. 647. Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  4. Leetcode 647. Palindromic Substrings

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  5. LeetCode——Palindromic Substrings

    Question Given a string, your task is to count how many palindromic substrings in this string. The s ...

  6. 647. Palindromic Substrings 互文的子字符串

    [抄题]: Given a string, your task is to count how many palindromic substrings in this string. The subs ...

  7. 【Leetcode】647. Palindromic Substrings

    Description Given a string, your task is to count how many palindromic substrings in this string. Th ...

  8. [LeetCode] 647. Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  9. LeetCode 647. 回文子串(Palindromic Substrings)

    647. 回文子串 647. Palindromic Substrings 题目描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符 ...

  10. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

随机推荐

  1. delphi将一个list中包含的元素,从另一个中删除,如果在另一个中存在的话

    Function StrList_Del(StrList,DelStrList:String):String; //将DelStrList中包含的元素,从Strlist中删除,如果在Strlist中存 ...

  2. [Note][深入理解Java虚拟机] 第三章 垃圾收集器与内存分配策略笔记

    书上关于GCTimeRatio的讲解有点难以理解,查看Oracle的文档后重新理解了下 -XX:GCTimeRatio 运行时间 / GC时间 当GCTimeRatio为19时,运行时间是GC时间的1 ...

  3. linux修改ulimit参数

    有如下三种修改方式: 1.在/etc/rc.local 中增加一行 ulimit -SHn 655352.在/etc/profile 中增加一行 ulimit -SHn 655353.在/etc/se ...

  4. Android开源界面库--ResideMenu用法

    网上关于ResideMenu用法的教程很多,但基本上全是从Github上copy下来的,Gitbub上给出的了对应的demo,但是由于我的IDE原因吧,demo一直导入不成功.为此自己又捣鼓了一翻,终 ...

  5. [Java]用于将链表变成字符串并在元素之间插入分隔符的有用函数“String.join”

    将链表变成字符串并在元素之间插入分隔符,这种动作最常见于组合sql文“select a,b,c from tb”这种场景scenario,其中a,b,c你是存贮在链表中的, 如果要加逗号要么在循环中识 ...

  6. vscode+python+flake8+cmder配置

    {     "window.zoomLevel": 0,     "[python]": {},     "kite.showWelcomeNotif ...

  7. 网络通信框架之okHttp

    主页: https://github.com/square/okhttp 特点: * 支持HTTP/2 和 SPDY * 默认支持 GZIP 降低传输内容的大小 * 支持网络请求的缓存 * 当网络出现 ...

  8. Java之HSF搭建demo

    1.去阿里云官网下载Demo edas-app-demo.zip 2.下载Ali-Tomcat和Pandora,注意红色下面字体 a)下载 Ali-Tomcat,保存后解压至相应的目录(如:d:\wo ...

  9. [idea]添加jar包的方法

    一:在项目的根目录下建立lib文件夹,然后将对应的jar包文件拷贝进去. 二:点击项目右键,选择Open Module Settings 三.选择Project Settings->Librar ...

  10. 七十:flask钩子函数之关于before_request的钩子函数

    在flask中钩子函数是使用特定的装饰器装饰的函数,用于在正常执行的代码中,插入一段自己想要执行的代码(hook) before_first_request:flask项目第一次部署后指向的钩子函数, ...