Given a String s, count all special palindromic substrings of size greater than 1. A Substring is called special palindromic substring if all the characters in the substring are same or only the middle character is different for odd length. Example “aabaa” and “aaa” are special palindromic substrings and “abcba” is not special palindromic substring.
Input : str = " abab" Output : 2 All Special Palindromic substring are: "aba", "bab" Input : str = "aabbb" Output : 4 All Special substring are: "aa", "bb", "bbb", "bb"
Simple Solution is that we simply generate all substrings one-by-one and count how many substring are Special Palindromic substring. This solution takes O(n3) time.
There are 2 Cases :
Case 1: All Palindromic substrings have same character :
We can handle this case by simply counting the same continuous character and using formula K*(K+1)/2 (total number of substring possible : Here K is count of Continuous same char).
Lets Str = "aaabba" Traverse string from left to right and Count of same char "aaabba" = 3, 2, 1 for "aaa" : total substring possible are 'aa' 'aa', 'aaa', 'a', 'a', 'a' : 3(3+1)/2 = 6 "bb" : 'b', 'b', 'bb' : 2(2+1)/2 = 3 'a' : 'a' : 1(1+1)/2 = 1
We can handle this case by storing count of same character in another temporary array called “sameChar[n]” of size n. and pick each character one-by-one and check its previous and forward character are equal or not if equal then there are min_between( sameChar[previous], sameChar[forward] ) substring possible.
Let's Str = "aabaaab" Count of smiler char from left to right : that we will store in Temporary array "sameChar" Str = " a a b a a a b " sameChar = 2 2 1 3 3 3 1 According to the problem statement middle character is different: so we have only left with char "b" at index :2 ( index from 0 to n-1) substring : "aabaaa" so only two substring are possible : "aabaa", "aba" that is min (smilerChar[index-1], smilerChar[index+1] ) that is 2.
Below is the implementation of above idea
Time Complexity : O(n)
Auxiliary Space : O(n)
- Count maximum-length palindromes in a String
- Program to count vowels, consonant, digits and special characters in string.
- Check if suffix and prefix of a string are palindromes
- Count Uppercase, Lowercase, special character and numeric values
- Move all special char to the end of the String
- Reverse a string without affecting special characters
- Split a string in equal parts such that all parts are palindromes
- Split numeric, alphabetic and special symbols from a String
- Check if a string can be rearranged to form special palindrome
- Find the count of palindromic sub-string of a string in its sorted form
- Count occurrences of a string that can be constructed from another given string
- Count subsequences in first string which are anagrams of the second string
- Program to print all palindromes in a given range
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of palindromes required to express N as a sum | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.