当前位置:网站首页>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 .
边栏推荐
- Perfect square
- CDN single page reference of indexbar index column in vant framework cannot be displayed normally
- Bi modal progressive mask attention for fine graded recognition
- [deep learning] fast Reid tutorial
- Opencv 15 face recognition and eye recognition
- Opencv 07, pixel read, change and bitmap write
- Queuing theory, game theory, analytic hierarchy process
- Data warehouse notes | 5 factors that need attention for customer dimension modeling
- The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)
- Matlab: obtain the figure edge contour and divide the figure n equally
猜你喜欢
Branch and bound method, example sorting
04 route jump and carry parameters
Impossible d'afficher le contenu de la base de données après que l'idée a utilisé le pool de connexion c3p0 pour se connecter à la base de données SQL
[reading some papers] introducing deep learning into the public horizon alexnet
Useful websites for writing papers and studying at ordinary times
Mean Value Coordinates
[data and Analysis Visualization] data operation in D3 tutorial 3-d3
数仓笔记|针对客户维度建模需要关注的5个因素
Laravel 权限导出
Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
随机推荐
04 route jump and carry parameters
The latest Matlab r2020 B ultrasonic detailed installation tutorial (with complete installation files)
Professional database management software: Valentina Studio Pro for Mac
Using binary heap to implement priority queue
[deep learning] fast Reid tutorial
智能安全配电装置如何减少电气火灾事故的发生?
PCR validation of basic biological experiments in [life sciences]
CDN single page reference of indexbar index column in vant framework cannot be displayed normally
SANs证书生成
[reading papers] visual convolution zfnet
Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
Redis multiple servers share one
Perfect square
After idea uses c3p0 connection pool to connect to SQL database, database content cannot be displayed
[dest0g3 520 orientation] dest0g3, which has been a long time since we got WP_ heap
Matlab: find the inner angle of n-sided concave polygon
Impossible d'afficher le contenu de la base de données après que l'idée a utilisé le pool de connexion c3p0 pour se connecter à la base de données SQL
Vant realizes the adaptation of mobile terminal
Kotlin memorandum
JS deconstruction assignment