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)
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.
- Count maximum-length palindromes in a String
- Count of ways to split given string into two non-empty palindromes
- Check if suffix and prefix of a string are palindromes
- Split a string in equal parts such that all parts are palindromes
- Program to print all palindromes in a given range
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of palindromes required to express N as a sum | Set 2
- Program to count vowels, consonant, digits and special characters in string.
- Count Uppercase, Lowercase, special character and numeric values
- Count the number of Special Strings of a given length N
- Reverse a string without affecting special characters
- Split numeric, alphabetic and special symbols from a String
- Check if a string can be rearranged to form special palindrome
- Move all special char to the end of the String
- Calculate Sum of ratio of special characters to length of substrings of the given string
- Check if a string contains uppercase, lowercase, special characters and numeric values
- Check if a string consists only of special characters
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Sudo Placement | Special Subsequences
- Check if count of Alphabets and count of Numbers are equal in the given String
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.