Level:

  Medium

题目描述:

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".

思路分析:

  将字符串中每个字符看成回文串中间的数,然后向两边扩展,由于串的长可能为奇数也可能为偶数,所以都得考虑

代码:

public class Solution{
int res;
public int countSubstrings(String s){
if(s==null||s.length()==0)
return 0;
for(int i=0;i<s.length();i++){
help(i,i,s);
help(i,i+1,s);
}
return res;
}
public void help(int start,int end,String s){
while(start>=0&&end<s.length()&&s.charAt(start)==s.charAt(end)){
start--;
end++;
res++;
}
}
}

68.Palindromic Substrings(回文字符串的个数)的更多相关文章

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

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

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

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

  3. LeetCode 5. Longest Palindromic Substring & 回文字符串

    Longest Palindromic Substring 回文这种简单的问题,在C里面印象很深啊.希望能一次过. 写的时候才想到有两种情况: 454(奇数位) 4554(偶数位) 第1次提交 cla ...

  4. [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  5. [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  6. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  7. leetcode 5 Longest Palindromic Substring--最长回文字符串

    问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  8. 转载-----Java Longest Palindromic Substring(最长回文字符串)

    转载地址:https://www.cnblogs.com/clnchanpin/p/6880322.html 假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic st ...

  9. Longest Palindromic Substring (最长回文字符串)——两种方法还没看,仍需认真看看

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

随机推荐

  1. TCP: time wait bucket table overflow

    .TCP: time wait bucket table overflow tcp的连接数超出了服务器设置的连接数 1 2 3 4 5 6 [root@test log]# netstat -antp ...

  2. HDU 1373 XYZZY (spfa的特殊用法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1317 题目大意:有n个房间,编号1-n,房间之间有单向门连接.某人初始位于1号房间,且具有100点能量 ...

  3. R语言 Keras Training Flags

    在需要经常进行调参的情况下,可以使用 Training Flags 来快速变换参数,比起直接修改模型参数来得快而且不易出错. https://tensorflow.rstudio.com/tools/ ...

  4. 英语单词character

    来源——tr帮助说明 TR() User Commands TR() NAME tr - translate or delete characters SYNOPSIS tr [OPTION]... ...

  5. namedtuple的简单使用

    """ factory function for creating tuple subclasses with named fields namedtuple 是tupl ...

  6. 如何在Web页面里面使用高拍仪扫描上传图像

    问题: 在网页上,客户端访问的时候,可以扫描图象(通过扫描仪),并放到网页上,上传到服务器,如何实现?就是提供扫描仪的驱动程序,并使用扫描仪来扫描图象 ,有没有此类的ActiveX控件 回复: 目前大 ...

  7. POJ 3660 Cow Contest ( 最短路松弛思想应用 && Floyd求传递闭包 )

    题意 : 给出 N 头奶牛在比赛的结果,问你最多的能根据给出结果确定其名次的奶牛头数.结果给出的形式为 A  B 代表在比赛当中 A 战胜了 B 分析 : 对于一头奶牛来说,如果我们能确定其他 N - ...

  8. Pycharm创建模板头部默认

    PyCharm 打开,点击左上角 “FILE” 进入 “Settings”,进行头文件设置: 如下: 我的模板: #!/usr/bin/env python# -*- coding:utf-8 -*- ...

  9. Java后端进阶教程

    https://www.cnblogs.com/caoleiCoding/p/6170555.html 传智播客Java Spring框架:https://www.bilibili.com/video ...

  10. css中div垂直居中的方法。

    利用绝对定位实现的居中 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...