Palindromes · Medium
A text-analysis tool counts contiguous ranges that read the same forward and backward. It counts positions rather than distinct strings: identical text occurring in different ranges represents different observations.
Count all non-empty palindromic substrings by their start and end positions.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ text: string }
Fields
- text is an ASCII string. Case, punctuation and spaces are significant; do not normalize or trim it.
Output
Return value
- Return an integer count. Every one-character range counts. Empty text returns 0.
- Do not count the empty string or merge ranges with equal text.
Constraints
- 0 <= text.length <= 2000; ASCII only.
Examples
Example 1
{
"text": "aaa"
}Returns
6Example 2
{
"text": "abba"
}Returns
6Required complexity
O(n^2) time and O(1) auxiliary space; faster exact approaches are also accepted