当前位置:网站首页>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 .
边栏推荐
- wx.createSelectorQuery()在components获取Dom节点的使用
- 数字IC设计——FIFO的设计
- Data processing in detailed machine learning (II) -- Feature Normalization
- 02 优化微信开发者工具默认的结构
- Find the number of permutations
- Welcome to blog navigation
- [reading papers] comparison of deeplobv1-v3 series, brief review
- Use of OpenCV 12 findcircuits and drawcircuits
- [data analysis and visualization] key points of data drawing 3- spaghetti map
- js 解构赋值
猜你喜欢

Opencvshare4 and vs2019 configuration

Useful websites for writing papers and studying at ordinary times

Huffman tree and its application

智能安全配电装置如何减少电气火灾事故的发生?

Laravel 权限导出

專業的數據庫管理軟件:Valentina Studio Pro for Mac

OpenCVSharpSample04WinForms

Uni app Foundation
![HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)](/img/c9/884aa008a185a471dfe252c0756fc1.png)
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)

A real-time target detection model Yolo
随机推荐
The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)
Image classification system based on support vector machine (Matlab GUI interface version)
[reading point paper] deeplobv3 rethinking atlas revolution for semantic image segmentation ASPP
[data analysis and visualization] key points of data drawing 6- too many data groups
[reading papers] visual convolution zfnet
[data analysis and visualization] key points of data mapping 7- over mapping
wx. Createselectorquery() gets the usage of DOM nodes in components
Ijkplayer source code - rendering
Data processing in detailed machine learning (II) -- Feature Normalization
[data analysis and visualization] key points of data drawing 10- construction of legend
Professional database management software: Valentina Studio Pro for Mac
Digital IC Design -- FIFO design
微信云开发粗糙理解
OpenCVSharpSample04WinForms
小程序 input,textarea组件权重比fixed的z-index都高
[reading paper] generate confrontation network Gan
Use of OpenCV 11 kmeans clustering
Simple use of leaflet - offline map scheme
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
Laravel 权限导出