68.Palindromic Substrings(回文字符串的个数)
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(回文字符串的个数)的更多相关文章
- [LeetCode] Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- [LeetCode] 647. Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- LeetCode 5. Longest Palindromic Substring & 回文字符串
Longest Palindromic Substring 回文这种简单的问题,在C里面印象很深啊.希望能一次过. 写的时候才想到有两种情况: 454(奇数位) 4554(偶数位) 第1次提交 cla ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- leetcode 5 Longest Palindromic Substring--最长回文字符串
问题描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 转载-----Java Longest Palindromic Substring(最长回文字符串)
转载地址:https://www.cnblogs.com/clnchanpin/p/6880322.html 假设一个字符串从左向右写和从右向左写是一样的,这种字符串就叫做palindromic st ...
- Longest Palindromic Substring (最长回文字符串)——两种方法还没看,仍需认真看看
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
随机推荐
- 05XML
1.XML入门 1.1 引入 HTML, 超文本标记语言. html语言语法是很松散的! 1)标签不区分大小写的! 2)标签可以不匹配的. 由w3c组织,针对html的语法缺陷,去设计了另一门,叫xm ...
- CentOS安装Git服务器 Centos 6.5 + Git 1.7.1.0 + gitosis
1.安装扩展 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel 2.下载git ...
- Mybatis(三)MyBatis 注解方式的基本 用法
在 MyBatis注解 SQL 中,最基本的就是@Select.@Insert.@Update 和@Delete 四种.
- Spring---Spring Integration
1.概述 1.1.Spring Integration 提供了 基于spring 的 EIP(Enterprise Integration Patterns,企业集成模式)的实现: 1.2.Sp ...
- DataFrame.loc的区间
df.loc[dates[0:1],'E']和df.loc[dates[0]:dates[1],'E']不同. 前者不包含dates[1],后者是包含dates[1]的. 根据Python3中实际代码 ...
- $nextTick
Vue 实现响应式并不是数据发生变化之后 DOM 立即变化,而是按一定的策略进行 DOM 的更新. $nextTick 是在下次 DOM 更新循环结束之后执行延迟回调,在修改数据之后使用 $nextT ...
- ModelViewSet的用法
- 170819-关于JSTL的知识点
1.JSTL(JSP Standard Tag Library) [1] JSTL简介 > JSTL是JSP的标准标签库 > JSTL为我们提供了一些常用的标签,供我们日常开发使用(if ...
- 前端面试之路之HTML面试真题
1.doctype的意义是什么 让浏览器以标准模式渲染 让浏览器知道元素的合法性 2.HTML XHTML HTML5的关系 HTML属于SGML XHTML属于XML,是HTML进行XML严格化的结 ...
- HDU 3605 Escape(二分图多重匹配问题)
Escape Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...