Given a string S, count the number of non empty sub strings that are palindromes. A sub…
- User
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.
Assistant
We are given a problem: count the number of non-empty substrings that are palindromes. Input string length up to 50. Simple solution: iterate over all substrings and check if palindrome. Complexity O(N^3) is fine for N=50. But we can do better: expand around centers for O(N^2). However for N=50 it's fine. But sample: "dskjkd" -> 7. Let's verify: string: d s k j k d. Palindromic substrings: single letters: 6. Also "kjk" at positions 3-5 (1-indexed) is a palindrome. That's 7. So answer 7.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.