Palindromic Substrings
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的更多相关文章
- [LeetCode] Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- [Swift]LeetCode647. 回文子串 | Palindromic Substrings
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- 647. 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——Palindromic Substrings
Question Given a string, your task is to count how many palindromic substrings in this string. The s ...
- 647. Palindromic Substrings 互文的子字符串
[抄题]: Given a string, your task is to count how many palindromic substrings in this string. The subs ...
- 【Leetcode】647. Palindromic Substrings
Description Given a string, your task is to count how many palindromic substrings in this string. Th ...
- [LeetCode] 647. Palindromic Substrings 回文子字符串
Given a string, your task is to count how many palindromic substrings in this string. The substrings ...
- LeetCode 647. 回文子串(Palindromic Substrings)
647. 回文子串 647. Palindromic Substrings 题目描述 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子串,即使是由相同的字符 ...
- Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)
Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
随机推荐
- (70)一篇文章带你熟悉 TCP/IP 协议
作者:涤生_Woo链接:http://www.jianshu.com/p/9f3e879a4c9c來源:简书著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 同样的,本文篇幅也比较 ...
- [科普] CPU, GPU, TPU的区别
Google Cloud 原文链接:https://cloud.google.com/blog/products/ai-machine-learning/what-makes-tpus-fine-tu ...
- python中的定时器threading.Timer
由浅入深学SQL Server 2012 --> python开发中用到,定时操作.例如每隔1s执行一次,发现 threading.Timer,这个东西,可以直接用. 其原理为执行函数中置定时 ...
- js监听某个元素高度变化来改变父级iframe的高度
最近需要做一个iframe调用其他页面内容,这个iframe地址是可变化的,但是里面的内容高度不确定且里面内容高度可调整,所以需要通过监听iframe里面body的高度变化来调整iframe的高度. ...
- *CodeIgniter框架集成支付宝即时到账SDK
客户的网站需要支付功能,我们选择了业界用的最多的支付宝即时到账支付.申请了两次将近两周的时间终于下来了,于是我开始着手测试SDK整合支付流程. SDK中的代码并不复杂,就是构造请求发送,接收并验证签名 ...
- js向input的value赋值
js与jquery:在我印象里面都是一样的,今天利用空闲的时间来总结一下,js与jquery究竟有什么区别? js : 是一门网页的脚本语言 jquery :jquery是基于js的一种框架,也就是说 ...
- oracle表结构表数据导入导出
--------------------------------------imp/exp------------------------------------------------------- ...
- user_tab_columns和user_col_comments区别
SELECT USER_TAB_COLUMNS.COLUMN_NAME, USER_COL_COMMENTS.COMMENTS, CASE WHEN INSTR(USER_TAB_COLUMNS.DA ...
- 小D课堂 - 新版本微服务springcloud+Docker教程_3-02CAP理论知识
笔记 2.分布式应用知识CAP理论知识 简介:讲解分布式核心知识CAP理论 CAP定理: 指的是在一个分布式系统中,Consistency(一致性). Availabi ...
- 如何用 putty 连接远程 Linux 系统
如何用 putty 连接远程 Linux 系统 Putty 简介 Putty 是一个免费的.Windows x86 平台下的 Telnet.SSH 和 Rlogin 客户端,但是功能丝毫不逊色于商业的 ...