Given a string s of length n, count the number of substrings having different types of palindromic characteristics.
Palindromic Characteristic is the number of k-palindromes in a string where k lies in range [0, n).
A string is 1-palindrome(or simply palindrome) if and only if it reads same the backward as it reads forward.
A string is k-palindrome (k > 1) if and only if :
1. Its left half is equal to its right half.
2. Its left half and right half should be non-empty (k – 1)-palindrome. The left half of a string of length ‘t’ is its prefix of length ⌊t/2⌋, and right half is the suffix of the same length.
Note : Each substring is counted as many times as it appears in the string. For example, in the string “aaa” the substring “a” appears 3 times.
Input : abba Output : 6 1 0 0 Explanation : "6" 1-palindromes = "a", "b", "b", "a", "bb", "abba". "1" 2-palindrome = "bb". Because "b" is 1-palindrome and "bb" has both left and right parts equal. "0" 3-palindrome and 4-palindrome. Input : abacaba Output : 12 4 1 0 0 0 0 Explanation : "12" 1-palindromes = "a", "b", "a", "c", "a", "b", "a", "aba", "aca", "aba", "bacab", "abacaba". "4" 2-palindromes = "aba", "aca", "aba", "abacaba". Because "a" and "aba" are 1-palindromes. NOTE : "bacab" is not 2-palindrome as "ba" is not 1-palindrome. "1" 3-palindrome = "abacaba". Because is "aba" is 2-palindrome. "0" other k-pallindromes where 4 < = k < = 7.
Approach : Take a string s and say it is a 1-palindrome and checking its left half also turns out to be a 1-palindrome then, obviously its right part will always be equal to left part (as the string is also a 1-palindrome) making the original string to be 2-palindrome. Now, similarly, checking the left half of the string, it also turns out to be a 1-palindrome then it will make left half to be 2-palindrome and hence, making original string to be 3-palindrome and so on checking it till only 1 alphabet is left or the part is not a 1-palindrome.
Lets take string = “abacaba”. As known “abacaba” is 1-palindrome. So, when checking for its left half “aba”, which is also a 1-palindrome, it makes “abacaba” a 2-palindrome. Then, check for “aba”‘s left half “a” which is also a 1-palindrome. So it makes “aba” a 2-palindrome and “aba” makes “abacaba” a 3-palindrome. So in other words, if a string is a k-palindrome then it is also a (k-1)-palindrome, (k-2)-palindrome and so on till 1-palindrome. Code for the same is given below which firsts check each and every substring if it is a palindrome or not and then counts the number of k-palindromic substrings recursively in logarithmic time.
Below is the implementation of above approach :
12 4 1 0 0 0 0
Time Complexity : O()
Auxiliary Space : O()
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Make palindromic string non-palindromic by rearranging its letters
- Longest Palindromic Substring using Palindromic Tree | Set 3
- Find the count of palindromic sub-string of a string in its sorted form
- Count All Palindromic Subsequence in a given String
- Permutation of given string that maximizes count of Palindromic substrings
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Count of unique palindromic strings of length X from given string
- Count of pairs of strings whose concatenation forms a palindromic string
- Minimum Count of Bit flips required to make a Binary String Palindromic
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Find a palindromic string B such that given String A is a subsequense of B
- Check if a string contains a palindromic sub-string of even length
- Make a palindromic string from given string
- Count of Palindromic substrings in an Index range
- Count all Prime Length Palindromic Substrings
- Count of odd length contiguous Palindromic sequences in a Matrix
- Count of N digit palindromic numbers divisible by 9
- Count of palindromic plus paths in a given Matrix
- Count of K-size substrings having palindromic permutations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manishshaw1