forked from Anubhav-1020/Hacktoberfest-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_palindrome_substr.cpp
47 lines (43 loc) · 1.27 KB
/
count_palindrome_substr.cpp
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
//Problem statement : string s is given. find out the number of palindromic substrings formed from the given string.
// -> let s : "aba"
// -> possible substrings : "a","b,","a","ab","ba","aba"
// -> total palindromic substrings : "a","b","a","aba" : 4 ---> output
#include<bits/stdc++.h>
using namespace std;
//function to count total number of palindrome substrings
int countPalisubstr(string s){
int count=0;
int dp[s.size()][s.size()];
// setting all values of matrix to -1
memset(dp,-1,sizeof(dp));
for(int i=0;i<s.size();i++){
int j=i;
dp[i][j] = 1;
count++;
}
for(int i=0;i<s.size()-1;i++){
int j=i+1;
if(s[i]==s[j]){
dp[i][j] = 1;
count++; // condition if length of string is 1
}
}
// condition if length of string is greater or equal to 2.
for(int k=2;k<s.size();k++){
for(int i=0;i<s.size()-2;i++){
if(dp[i+1][i+k-1]==1 && s[i]==s[i+k]){
count++;
}
}
}
return count; //final substrings to be counted for output
}
int main()
{
string s;
// input from user
cin>>s;
int n = countPalisubstr(s);
// total number of count will be given as:
cout<<n;
}