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) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
随机推荐
- psd缩略图生成上传解决方案
第一点:Java代码实现文件上传 FormFile file = manform.getFile(); String newfileName = null; String newpathname = ...
- Gym 100971D 单调栈
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- 13. Ajax技术
在传统的Web应用模式中,页面中用户的每一次操作都将触发一次返回Web服务器的HTTP请求,服务器进行相应的处理后,返回一个HTML页面的客户端.而在Ajax应用中,页面中的用户的操作将通过Ajax引 ...
- SDOI2015 寻宝游戏 | noi.ac#460 tree
题目链接:戳我 可以知道,我们相当于是把有宝藏在的地方围了一个圈,求这个圈最小是多大. 显然按照dfs序来遍历是最小的. 那么我们就先来一遍dfs序列,并且预处理出来每个点到根的距离(这样我们就可用\ ...
- Killer Problem (UVA 11898 )
Problem You are given an array of N integers and Q queries. Each query is a closed interval [l, r]. ...
- selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
抓取网页代码后,由于是在同一个li标签下,所以使用一次性抓取,所有的a标签,然后循环做不同的操作,但是抛出找不到元素异常. def office_page(_chrome: Chrome): sn = ...
- Riot.js——一个小而美的JS框架
Riot.js是什么? Riot 拥有创建现代客户端应用的所有必需的成分: "响应式" 视图层用来创建用户界面 用来在各独立模块之间进行通信的事件库 用来管理URL和浏览器回退按钮 ...
- ShareX的使用
给截图设置边框 https://github.com/ShareX/ShareX/issues/2509 TaskSettings-->Image-->Effects-->image ...
- Python中列表的使用
python中的列表与java中的数组非常类似,但使用方法比java中数组简单很多,python中的数据类型不需要显示声明,但在使用时必须赋值,列表元素下标从0开始 初始化列表(初始化一个包含五个元素 ...
- py matplotlib 多个figure同时画多个图以及多个图例多个折线图
图例负号乱码的问题 import numpy as np import matplotlib.pyplot as pltimport matplotlibplt.rcParams['axes.un ...