forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pallindrome Substrings.cpp
74 lines (55 loc) · 1.33 KB
/
Pallindrome Substrings.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Given a string S, count and return the number of substrings of S that are palindrome.
Single length substrings are also palindromes. You just need to calculate the count, not the substrings.
Input Format :
String S
Output Format :
count of palindrome substrings
Constraints :
1 <= Length of S <= 1000
Sample Input :
aba
Sample Output :
4
**Note : Here 4 corresponds to ("a","b","a","aba").
*/
#include <bits/stdc++.h>
using namespace std;
int countPalindromeSubstrings(char s[]) {
// Write your code here
string st(s);
int c=0;
//First Considering palindroms odd case
for(int i=0;i<st.size();i++){
int k=0;
while (i-k>=0 && i+k<st.size()){
if(st[i-k]==st[i+k])
c++;
else
break;
k++;
}
}
//Now considering the even palindroms
for(int i=0;i<st.size()-1;i++){
if(st[i]==st[i+1]){
int k=0;
while (i-k>=0 && i+1+k<st.size()){
if(st[i-k]==st[i+1+k])
c++;
else
break;
k++;
}
}
}
return c;
}
#include <iostream>
#include "solution.h"
using namespace std;
int main() {
char input[10000];
cin >> input;
cout << countPalindromeSubstrings(input) << endl;
}