Skip to content

Commit b2befbc

Browse files
Create Count Special Palindromic Substrings.cpp
1 parent 24105ce commit b2befbc

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Count Special Palindromic Substrings
3+
Send Feedback
4+
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:
5+
If all the characters in the substring are the same.
6+
7+
If the length of the substring is odd and only the middle element is different, while all the other characters are the same.
8+
Example:
9+
“aba” is a special palindrome, while “abaa” is not
10+
Input Format:
11+
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.
12+
13+
The first and the only line of each test case contains the string 'STR'.
14+
Output Format:
15+
For each test case, print the count of special palindromic substrings.
16+
17+
Print the output of each test case in a separate line.
18+
Note:
19+
You don’t need to print anything; It has already been taken care of. Just implement the given function.
20+
Constraints:
21+
1 <= T <= 100
22+
1 <= |STR| <= 10000
23+
24+
Time limit: 1 sec
25+
Sample Input 1:
26+
2
27+
bcbc
28+
ccddd
29+
Sample Output 1:
30+
2
31+
4
32+
Explanation For Sample Input 1:
33+
In the first test case,
34+
The special palindromic substrings in the given string are: bcb and cbc. Hence, the answer is 2 in this case.
35+
36+
In the second test case,
37+
The special palindromic substrings in the string are: cc, dd, ddd and dd. Hence, the answer is 4 in this case.
38+
Sample Input 2:
39+
2
40+
abccdcdf
41+
baabaab
42+
Sample Output 2:
43+
3
44+
4
45+
*/
46+
47+
48+
49+
/*
50+
Time complexity: O(N)
51+
Space complexity: O(N)
52+
53+
Where N is the size of the string.
54+
*/
55+
56+
57+
int specialPalindromes(string &str)
58+
{
59+
60+
// Initialise ans to store count of special palindromes
61+
int ans = 0;
62+
63+
// Initialise variable to store length of string
64+
int n = str.length();
65+
66+
// Create an array with all values 0 to store count of continuous same characters
67+
vector<int> countSame(n, 0);
68+
69+
// First pointer
70+
int a = 0;
71+
72+
// Start traversing through the string
73+
while(a < n)
74+
{
75+
76+
// Initialise a variable to store temporary count of continuous same characters
77+
int countContinuous = 1;
78+
79+
// Initialise a variable to store next character
80+
int b = a + 1;
81+
82+
// Count same characters
83+
while(b < n and str[a] == str[b] && (b < n))
84+
{
85+
countContinuous++;
86+
b++;
87+
}
88+
89+
// CASE: 1
90+
// Count total substrings from the count of continuous same characters
91+
ans = ans + (countContinuous * (countContinuous - 1) / 2);
92+
93+
// Store this count in the array
94+
countSame[a] = countContinuous;
95+
96+
a = b;
97+
}
98+
99+
// CASE: 2
100+
for(int i = 1; i < n; i++)
101+
{
102+
103+
// If current character is equal to previous character
104+
if(str[i] == str[i - 1])
105+
{
106+
107+
// Update values of array
108+
countSame[i] = countSame[i - 1];
109+
}
110+
111+
if(i > 0 && i < (n - 1) && (str[i - 1] == str[i + 1]) && (str[i] != str[i + 1]))
112+
{
113+
ans += min(countSame[i - 1], countSame[i + 1]);
114+
}
115+
}
116+
117+
// Return count
118+
return ans;
119+
}

0 commit comments

Comments
 (0)