当前位置:网站首页>String: number of substring palindromes
String: number of substring palindromes
2022-06-13 02:44:00 【Zeng Qiang】
List of articles
subject
Find the number of palindromes in all substrings of a string . for example
- “aaa”, Its substring is a palindrome including :a,a,a,aa,aa,aaa.
- “abcba", Is a substring of palindromes including :a,b,c,bcb,abcba,b,a
Their thinking
The original question can be divided into several small questions :
- How to get a substring ?
- Find the number of palindromes ?
Substring
Substring . The first way is , We started at both ends of the string , Move the double pointer inward , Judge whether there is palindrome in the interval . And here we are going to use the second method , And the center of the string spreads outward , Request palindrome .
Number of palindromes
Like strings s = “abcba”, We use c center , The pointer moves left and right 1 position , Get the substring “bcb”, So we know that this is a palindrome , The length is 3. Then continue to move the pointer to the left and right , Get a new palindrome abcba.
It's important to note that , The establishment of the central point . If the palindrome length is an odd number , Then there is only one character in the center of the substring ; Similarly, if the length of palindromes is even , There are two characters in the center of that substring .
According to the above ideas , We can get the following code :
Code
class Solution {
public int countSubstrings(String s) {
if(s == null || s.length() == 0) {
return 0;
}
int count = 0;
for(int i = 0; i < s.length(); i++) {
// Enumerate each character of the string , From left to right .
// Focus on each point , Spread to the left and right for palindromes .
count += countPalidrome(s, i, i); // Confirm the center point
count += countPalidrome(s, i, i + 1); // Consider the case where the palindrome length is even , Confirm that the center point is two numbers
}
return count;
}
private int countPalidrome(String s, int start, int end) {
int count = 0;
while(start >= 0 && end < s.length() && s.charAt(start) == s.charAt(end)) {
count++;
start--;
end++;
}
return count;
}
}
summary
Find the number of substring palindromes . You can take a substring centered , The way of outward diffusion , Move the pointer to find the number of palindromes . It should be noted that the selection of the center point is divided into two cases: even palindrome length and odd palindrome length .
边栏推荐
- [data analysis and visualization] key points of data drawing 5- the problem of error line
- Image classification system based on support vector machine (Matlab GUI interface version)
- Opencv 9 resize size change rotate rotate blur mean (blur)
- wx. Createselectorquery() gets the usage of DOM nodes in components
- 小程序 input,textarea组件权重比fixed的z-index都高
- Basic principle of bilateral filtering
- js多种判断写法
- Data warehouse notes | 5 factors that need attention for customer dimension modeling
- Opencvshare4 and vs2019 configuration
- [data analysis and visualization] key points of data drawing 6- too many data groups
猜你喜欢
![[data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge](/img/a8/468a0c4d4a009e155679898fac4b81.jpg)
[data and Analysis Visualization] D3 introductory tutorial 1-d3 basic knowledge

Useful websites for writing papers and studying at ordinary times

03 recognize the first view component
![[data analysis and visualization] key points of data drawing 8- use of circular bar chart](/img/1f/232f2f0134867eeec3f1cfe005b2b5.jpg)
[data analysis and visualization] key points of data drawing 8- use of circular bar chart

04路由跳转并携带参数

Opencv 15 face recognition and eye recognition
![[reading papers] visual convolution zfnet](/img/01/4181f19b2d24b842488522c2001970.jpg)
[reading papers] visual convolution zfnet
![[reading papers] deep learning face representation from predicting 10000 classes. deepID](/img/40/94ac64998a34d03ea61ad0091a78bf.jpg)
[reading papers] deep learning face representation from predicting 10000 classes. deepID
![[reading papers] dcgan, the combination of generating countermeasure network and deep convolution](/img/31/8c225627177169f1a3d6c48fd7e97e.jpg)
[reading papers] dcgan, the combination of generating countermeasure network and deep convolution

Detailed explanation of handwritten numeral recognition based on support vector machine (Matlab GUI code, providing handwriting pad)
随机推荐
Hstack, vstack and dstack in numpy
Simple use of leaflet - offline map scheme
Rough understanding of wechat cloud development
04路由跳转并携带参数
Several articles on norms
Data warehouse notes | 5 factors that need attention for customer dimension modeling
Detailed installation tutorial of MATLAB r2019 B-mode ultrasound (complete installation files are attached)
Opencvsharp4 handwriting recognition
Linear, integer, nonlinear, dynamic programming
[reading point paper] yolo9000:better, faster, stronger, (yolov2), integrating various methods to improve the idea of map and wordtree data fusion
JS deconstruction assignment
01 初识微信小程序
重定向设置参数-RedirectAttributes
[dest0g3 520 orientation] dest0g3, which has been a long time since we got WP_ heap
OneNote User Guide (1)
[data and Analysis Visualization] data operation in D3 tutorial 3-d3
[data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3
02 optimize the default structure of wechat developer tools
在IDEA使用C3P0连接池连接SQL数据库后却不能显示数据库内容
After idea uses c3p0 connection pool to connect to SQL database, database content cannot be displayed