Problem 395677 · medium · Phase 03 Linear Management & Searching

Count Mirrored Slices

strings · palindromes · counting

Given a string s, count how many of its substrings are palindromes. Substrings at different positions count separately even if their text is identical; every single character counts as one.

Examples

Input:  s = "abba"
Output: 6
Explanation: "a", "b", "b", "a", "bb", "abba".

Input:  s = "abc"
Output: 3

Constraints

  • 0 <= len(s) <= 2000
  • Target: O(n^2) time, O(1) extra space.

Goals

  • Count palindromic substrings by position, not by distinct content
  • Reuse the expand-around-centre idea to count instead of to find
  • Verify counts on tiny inputs by hand
Starting Python…