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. js中彻底删除对象属性

    delete运算符可以删除对象的属性,但是仅仅是断开了属性和宿主对象的联系,而不会去操作被删除属性的属性值,所以就需要彻底删除的出现 function myDelete(obj,propertyNam ...

  2. CF990G GCD Counting 点分治+容斥+暴力

    只想出来 $O(nlogn\times 160)$ 的复杂度,没想到还能过~ Code: #include <cstdio> #include <vector> #includ ...

  3. Android_(自动化)自动获取手机电池的剩余电量

    自动获取手机电池的剩余电量 通过使用BroadcastReceiver的特性来获取手机电池的电量,注册BroadcastReceiver时设置的IntentFilter来获取系统发出的Intent.A ...

  4. PHP-异常-1

    PHP 错误处理 在 PHP 中,默认的错误处理很简单.一条消息会被发送到浏览器,这条消息带有文件名.行号以及一条描述错误的消息. 在创建脚本和 web 应用程序时,错误处理是一个重要的部分.如果您的 ...

  5. ARTS打卡计划第六周

    Algorithms: https://leetcode-cn.com/problems/longest-palindromic-substring/ 中心扩展法首先考虑,当然看到有个动态规划,一直很 ...

  6. zookeeper系列(八)zookeeper客户端的底层详解

    作者:leesf    掌控之中,才会成功:掌控之外,注定失败.出处:http://www.cnblogs.com/leesf456/p/6098255.html 尊重原创,共同学习进步:  一.前言 ...

  7. SRS之RTMP连接处理线程conn:接收客户端推流

    由 SRS之RTMP的TCP线程 分析可知,SRS 接受客户端的连接后创建了一个线程:conn,用于处理与客户端的 RTMP 连接. 本文的分析是基于该配置文件的: listen 1935; max_ ...

  8. State Threads之Co-routine的调度

    1. 相关结构体 1.1 _st_epoll_data static struct _st_epolldata { _epoll_fd_data_t *fd_data; /* 调用 epoll_wai ...

  9. Ubuntu的root用户通过SSH登录的解决方法

    通过ISO镜像安装完ubuntu 系统后,我们不能直接使用putty或其他远程工具通过SSH方式连接到linux系统 原因: linux 系统上没有安装远程工具openssh-server 解决方法: ...

  10. 【黑马JavaSE】1_1_9_流程控制:顺序结构、判断结构、选择结构、循环结构

    文章目录 1_1_9_01_ 流程控制概述 1_1_9_01_ 顺序结构 1_1_9_ 判断结构 1_1_9_02_ 判断语句1--if 1_1_9_03_ 判断语句2--if...else 1_1_ ...