forked from imdhanish/HackerEarth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindrome Count.cpp
More file actions
66 lines (60 loc) · 1.49 KB
/
Palindrome Count.cpp
File metadata and controls
66 lines (60 loc) · 1.49 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Partially Accepted Solved, refer earlier commit to see where I went wrong
/*
Given a string S, count the number of non empty sub strings that are palindromes.
A sub string is any continuous sequence of characters in the string.
A string is said to be palindrome, if the reverse of the string is same as itself.
Two sub strings are different if they occur at different positions in S
Input
Input contains only a single line that contains string S.
Output
Print a single number, the number of sub strings that are palindromes.
Constraints
1 <= |S| <= 50
S contains only lower case latin letters, that is characters a to z.
SAMPLE INPUT
dskjkd
SAMPLE OUTPUT
7
Explanation
The 7 sub strings are d, s, k, j, k, d, kjk.
URL: https://www.hackerearth.com/practice/algorithms/dynamic-programming/2-dimensional/practice-problems/algorithm/palindrome-count-1/
*/
#include <iostream>
using namespace std;
bool ispalindrome(string input)
{
if(input == string(input.rbegin(), input.rend()))
{
return true;
}
else
{
return false;
}
}
int count(string s)
{
string temp;
int length = s.length();
int cnt;
for(int i = 0; i < length; i++)
{
for(int j = 1; j<= length - i ; j++)
{
temp = s.substr(i,j);
//int k = i + j - 1;
if(ispalindrome(temp))
{
cnt++;
}
}
}
return cnt;
}
int main()
{
string s;
cin >> s;
cout << count(s);
return 0;
}