-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount Palindrome Sub-Strings of a String .cpp
More file actions
50 lines (45 loc) · 1.07 KB
/
Count Palindrome Sub-Strings of a String .cpp
File metadata and controls
50 lines (45 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
int countPalin(string, int);
int main(){
int t, n;
string str;
cin >> t;
while(t--){
cin >> n >> str;
cout << countPalin(str, n) << endl;
}
return 0;
}
int countPalin(string str, int n){
bool arr[n][n];
int count = 0;
for(int i = 0; i < n; i++)
arr[i][i] = true;
for(int l = 2; l <= n; l++){
for(int i = 0; i < n - l + 1; i++){
int j = i + l - 1;
if(l == 2){
if(str[i] == str[j]){
arr[i][j] = true;
count++;
}
else
arr[i][j] = false;
}
else{
if(str[i] != str[j])
arr[i][j] = false;
else{
if(arr[i + 1][j - 1] == true){
arr[i][j] = true;
count++;
}
else
arr[i][j] = false;
}
}
}
}
return count;
}