EasyPythonJavaScriptTypeScript
DP — Count Palindromic Substrings
AlgorithmsDynamic ProgrammingStrings
solve(s) — return how many substrings of s are palindromes. Substrings with
different positions count separately, even when their text is identical.
Same idea as the longest palindromic substring, but every successful expansion step
adds one more palindrome to the count.
Examples
solve("abc") → 3 // "a", "b", "c"
solve("aaa") → 6 // "a" ×3, "aa" ×2, "aaa"
solve("abba") → 6 // "a", "b", "b", "a", "bb", "abba"
solve("racecar") → 10Sample tests
Test #1Only single letters
Input: ["abc"]
Output: 3
Test #2All the same letter
Input: ["aaa"]
Output: 6
Test #3Single character
Input: ["a"]
Output: 1