You are given a string 'STR'. Your task is to count the number of special palindromic substrings of size greater than 1 that the given string contains. A substring is said to be a special palindrome in the following two cases:
If all the characters in the substring are the same.
If the length of the substring is odd and only the middle element is different, while all the other characters are the same.
Example:
“aba” is a special palindrome, while “abaa” is not
The first line contains an integer ‘T’, which denotes the number of test cases or queries to be run. Then the test cases are as follows.
The first and the only line of each test case contains the string 'STR'.
Output Format:
For each test case, print the count of special palindromic substrings.
Print the output of each test case in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= |STR| <= 10000
Time limit: 1 sec
2
bcbc
ccddd
2
4
In the first test case,
The special palindromic substrings in the given string are: bcb and cbc. Hence, the answer is 2 in this case.
In the second test case,
The special palindromic substrings in the string are: cc, dd, ddd and dd. Hence, the answer is 4 in this case.
2
abccdcdf
baabaab
3
4
Can you think of checking each substring?
The basic approach is to create all possible substrings from the given string and then count the number of special palindromic substrings from them.
Algorithm:
O(N ^ 3), where N is the size of the string.
Since we are traversing through the complete string using three loops. Hence, the overall Time Complexity is O(N ^ 3).
O(1)
Constant extra space is required. Hence, the overall Space Complexity is O(1).