Top 150 InterviewMedium

Palindromic Substrings

Detailed guide and Python implementation for the 'Palindromic Substrings' problem.

Problem Statement

Medium

Given a string s, return the number of palindromic substrings in it.

A string is a palindrome when it reads the same backward as forward. A substring is a contiguous sequence of characters within the string.

Write a function countSubstrings(s: str) -> int.

Constraints
  • 1 <= len(s) <= 1000
  • s consists of lowercase English letters

Examples

Example 1
Input
s = "abc"
Output
3
Explanation

Three palindromic substrings: "a", "b", "c".

Example 2
Input
s = "aaa"
Output
6
Explanation

Six palindromic substrings: "a", "a", "a", "aa", "aa", "aaa".

Need a Hint?
Consider using 1D DP-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.