Hacker Earth Problems - Palindrome count
Problem Statement:
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
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.
Input contains only a single line that contains string S.
Output
Print a single number, the number of sub strings that are palindromes.
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.
1 <= |S| <= 50
S contains only lower case latin letters, that is characters a to z.
Explanation
The 7 sub strings are d, s, k, j, k, d, kjk.
Time Limit: 3 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Source Code:
Language: Python
s = raw_input ()
print sum (1 for i in range (len (s)) for j in range (i + 1, len (s) + 1) if s [i:j] == s [i:j][::-1])
print sum (1 for i in range (len (s)) for j in range (i + 1, len (s) + 1) if s [i:j] == s [i:j][::-1])
Comments
Post a Comment